How can I access a Spaces file’s metadata using the AWS SDK?
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!
Hi there,
You can access a file’s metadata in DigitalOcean Spaces by using the AWS SDK for S3, as Spaces is S3-compatible. Here’s an example in JavaScript using the AWS SDK for Node.js:
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'nyc3'}); // update to your region
// Create S3 service object with DigitalOcean Spaces endpoint
var s3 = new AWS.S3({
endpoint: 'nyc3.digitaloceanspaces.com', // update to your Spaces endpoint
accessKeyId: 'YOUR_ACCESS_KEY', // replace with your access key
secretAccessKey: 'YOUR_SECRET_KEY' // replace with your secret key
});
var params = {
Bucket: 'your-bucket-name',
Key: 'your-object-key'
};
s3.headObject(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
In this code, s3.headObject(params, ...) is used to retrieve the metadata of an object. The data object in the callback will contain a Metadata field that contains the metadata of the object.
Remember to replace 'YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY', 'your-bucket-name', and 'your-object-key' with your own values. The 'your-object-key' is the file name in the bucket.
Note: AWS SDK uses the us-east-1 region by default. Since DigitalOcean Spaces doesn’t use AWS regions, you can put any string there when configuring the AWS SDK, but it cannot be left blank.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.