zerouno
By:
zerouno

How to list files in Spaces

January 10, 2018 517 views
API Ubuntu 16.04

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...) 
1 Answer

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/',
]);
Have another answer? Share your knowledge.