Question

How to list files in Spaces

Hi, I have set up a Spaces and I can add files to it using laravel and S3 API package. Now I need to list my file to check if the modified date is 1 month old and then delete that file. I have tried to list object but I get:

Error executing "ListObjects" on "https://ams3.digitaloceanspaces.com/my-space%2Fbackups%2Fmy-folder?prefix=&delimiter=%2F&encoding-type=url"; AWS HTTP error: Client error: `GET https://ams3.digitaloceanspaces.com/my-space%2Fbackups%2Fmy-folder?prefix=&delim  
 iter=%2F&encoding-type=url` resulted in a `404 Not Found` response:                                                                                                                                                                                               
 <?xml version="1.0" encoding="UTF-8"?><Error><Code>NoSuchKey</Code><BucketName>my-space</BucketName><RequestId>tx0000000 (truncated...)                                                                                                                           
  NoSuchKey (client):  - <?xml version="1.0" encoding="UTF-8"?><Error><Code>NoSuchKey</Code><BucketName>my-space</BucketName><RequestId>tx0000000000000002405a5-005a5605e4-7bd0-ams3a</RequestId><HostId>7bd0-ams3a-ams3</HostId></Error>                          
                                                                                                                                                                                                                                                                   

In RequestException.php line 113:
                                                                                                                                                                        
 Client error: `GET https://ams3.digitaloceanspaces.com/my-space%2Fbackups%2Fmy-folder?prefix=&delimiter=%2F&encoding-type=url` resulted in a `404 Not Found` response:  
 <?xml version="1.0" encoding="UTF-8"?><Error><Code>NoSuchKey</Code><BucketName>my-space</BucketName><RequestId>tx0000000 (truncated...) 

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

If you could share the code that you are using to list the files, it might help us give you a better answer. Though just from the URL of the request, I think I can hopefully point you in the right direction.

In order to be compatible with existing tools, the Spaces API was designed to be inter-operable with the S3 API. With S3, there is no real concept of a folder. When you create a “folder” through the Spaces UI, it is actually just a “key” with a zero sized “object.” Items inside the folder have the folder key as a prefix to their own key.

To list the contents of a Space with the API, you can send a GET request to nyc3.digitaloceanspaces.com/$SPACE_NAME and it will return a the entire contents of the Space.

Your request is being sent to https://ams3.digitaloceanspaces.com/my-space/backups/my-folder instead, you should be sending /backups/my-folder/ as the “prefix” so that the response will only contain the objects with keys beginning with that string. You’re getting a 404 as the object /backups/my-folder doesn’t exist even if you have an object with a name like /backups/my-folder/file.ext

That’s a lot of detail that you might not really need. Here’s an example: If you’re using the AWS S3 SDK, it might look like:

$result = $s3Client->listObjects([
    'Bucket' => 'examplebucket',
    'Prefix' => '/backups/my-folder/',
]);