gaganbm
gaganbm

Reputation: 2843

How to set metadata in S3 using boto?

I am trying to set metadata during pushing a file to S3.

This is how it looks like :

def pushFileToBucket(fileName, bucket, key_name, metadata):
    full_key_name = os.path.join(fileName, key_name)
    k = bucket.new_key(full_key_name)
    k.set_metadata('my_key', 'value')
    k.set_contents_from_filename(fileName)

For some reason this throws error at set_metadata saying :

boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
<?xml version="1.0" encoding="UTF-8"?><Error><Code>SignatureDoesNotMatch</Code></Error>

And when I remove this set_metadata part, the file is getting stored correctly. Not sure what I am doing wrong. If the access key was invalid, then it wouldn't have saved the file anyway!

Upvotes: 2

Views: 3971

Answers (2)

sshevlyagin
sshevlyagin

Reputation: 1387

Another approach for someone using upload_file:

s3 = boto3.client('s3')
path = 'foo/bar.json'
file = 'bar.json'
bucket_name = 'foobar_bucket'
extra_args = {'CacheControl': 'max-age=86400'}
s3.upload_file(path, bucket_name, file_name, extra_args)

This would set the Cache-Control header on the file.

Upvotes: 4

gaganbm
gaganbm

Reputation: 2843

Got this fixed. Apparently we cannot have an underscore in the metadata key name.

Upvotes: 3

Related Questions