DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Redefining DevOps: The Transformative Power of Containerization
  • Build a Simple Chat Server With gRPC in .Net Core
  • What Is Test Pyramid: Getting Started With Test Automation Pyramid
  • Why You Should Consider Using React Router V6: An Overview of Changes
  1. DZone
  2. Data Engineering
  3. Databases
  4. How To: Set and Retrieve Metadata of an S3 Object

How To: Set and Retrieve Metadata of an S3 Object

Vijaykishan Shyamsundar user avatar by
Vijaykishan Shyamsundar
·
Jan. 14, 20 · Tutorial
Like (3)
Save
Tweet
Share
27.33K Views

Join the DZone community and get the full member experience.

Join For Free

In the article we will see how to upload the metadata of an object while uploading to S3 programatically.

I will be using Node.js to demonstrate the same.

Before jumping to that let’s see what is metadata and it’s use case

We have 2 types of metadata

  1. System Metadata    E.g. Date, Content-Length
  2. User defined metadata

While we upload the object, we can add custom metadata about the object along that as a key-value pair

 

Note: All user-defined metadata keys in lowercase

 

Let’s say we are having a platform where contents are added by multiple organization. In this scenario adding OrganizationID as one of the metadata will be handy.

Following code helps you to upload an object to s3 along it’s metadata.

JavaScript




x
43


 
1
var AWS = require('aws-sdk')
2
var credentials = require('./cred');
3
var s3 = new AWS.S3({accessKeyId: credentials.accesskey,
4
    secretAccessKey: credentials.secretkey,region:credentials.region});
5
var fs = require('fs');
6
 
          
7
fs.readFile('file2.txt', function (err, data) {
8
    if (err) { throw err; }
9
    var base64data = new Buffer(data, 'binary');
10
    s3.putObject({
11
        Bucket: 'uploadmetatest',
12
        Key: 'vijaykishan.txt',
13
        Metadata: {OrgID: 'TEKKIHUT', PROFILE: 'HD'},
14
        Body: base64data,
15
        ACL: 'public-read'
16
    }, function (resp) {
17
        console.log('Successfully uploaded package.');
18
    });
19
});


 

Let’s see how to retrieve the metadata of an uploaded object using node.js.

Following code helps you to retrieve the metadata of an object uploaded to s3.

JavaScript
xxxxxxxxxx
1
25
 
1
var params = {
2
    Bucket: "uploadmetatest",
3
    Key: "file2.txt"
4
};
5
s3.headObject(params, function (err, data) {    if (err) console.log(err, err.stack); // an error occurred
6
    else {
7
        console.log(data.Metadata['orgid']);
8
        console.log(data.Metadata['profile']);
9
   }       
10
});

 

Note

The PUT request header is limited to 8 KB in size. Within the PUT request header, the user-defined metadata is limited to 2 KB in size. The size of user-defined metadata is measured by taking the sum of the number of bytes in the UTF-8 encoding of each key and value.

 

Hope this helps

Metadata AWS Object (computer science)

Opinions expressed by DZone contributors are their own.

Trending

  • Redefining DevOps: The Transformative Power of Containerization
  • Build a Simple Chat Server With gRPC in .Net Core
  • What Is Test Pyramid: Getting Started With Test Automation Pyramid
  • Why You Should Consider Using React Router V6: An Overview of Changes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: