add headers param to s3 uploads

This commit is contained in:
Ash Caire 2015-06-30 23:04:15 +10:00
parent 4ef5a45347
commit 3ccf2e8a86

View file

@ -56,6 +56,11 @@ options:
required: false required: false
default: 600 default: 600
aliases: [] aliases: []
headers:
description:
- Custom headers for PUT operation, as a dictionary of 'key=value' and 'key=value,key=value'.
required: false
default: null
marker: marker:
description: description:
- Specifies the key to start with when using list mode. Object keys are returned in alphabetical order, starting with key after the marker in order. - Specifies the key to start with when using list mode. Object keys are returned in alphabetical order, starting with key after the marker in order.
@ -147,6 +152,9 @@ EXAMPLES = '''
# PUT/upload with metadata # PUT/upload with metadata
- s3: bucket=mybucket object=/my/desired/key.txt src=/usr/local/myfile.txt mode=put metadata='Content-Encoding=gzip,Cache-Control=no-cache' - s3: bucket=mybucket object=/my/desired/key.txt src=/usr/local/myfile.txt mode=put metadata='Content-Encoding=gzip,Cache-Control=no-cache'
# PUT/upload with custom headers
- s3: bucket=mybucket object=/my/desired/key.txt src=/usr/local/myfile.txt mode=put headers=x-amz-grant-full-control=emailAddress=owner@example.com
# List keys simple # List keys simple
- s3: bucket=mybucket mode=list - s3: bucket=mybucket mode=list
@ -285,7 +293,7 @@ def path_check(path):
return False return False
def upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt): def upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt, headers):
try: try:
bucket = s3.lookup(bucket) bucket = s3.lookup(bucket)
key = bucket.new_key(obj) key = bucket.new_key(obj)
@ -293,7 +301,7 @@ def upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt):
for meta_key in metadata.keys(): for meta_key in metadata.keys():
key.set_metadata(meta_key, metadata[meta_key]) key.set_metadata(meta_key, metadata[meta_key])
key.set_contents_from_filename(src, encrypt_key=encrypt) key.set_contents_from_filename(src, encrypt_key=encrypt, headers=headers)
url = key.generate_url(expiry) url = key.generate_url(expiry)
module.exit_json(msg="PUT operation complete", url=url, changed=True) module.exit_json(msg="PUT operation complete", url=url, changed=True)
except s3.provider.storage_copy_error, e: except s3.provider.storage_copy_error, e:
@ -367,6 +375,7 @@ def main():
dest = dict(default=None), dest = dict(default=None),
encrypt = dict(default=True, type='bool'), encrypt = dict(default=True, type='bool'),
expiry = dict(default=600, aliases=['expiration']), expiry = dict(default=600, aliases=['expiration']),
headers = dict(type='dict'),
marker = dict(default=None), marker = dict(default=None),
max_keys = dict(default=1000), max_keys = dict(default=1000),
metadata = dict(type='dict'), metadata = dict(type='dict'),
@ -390,6 +399,7 @@ def main():
expiry = int(module.params['expiry']) expiry = int(module.params['expiry'])
if module.params.get('dest'): if module.params.get('dest'):
dest = os.path.expanduser(module.params.get('dest')) dest = os.path.expanduser(module.params.get('dest'))
headers = module.params.get('headers')
marker = module.params.get('marker') marker = module.params.get('marker')
max_keys = module.params.get('max_keys') max_keys = module.params.get('max_keys')
metadata = module.params.get('metadata') metadata = module.params.get('metadata')
@ -533,24 +543,24 @@ def main():
if md5_local == md5_remote: if md5_local == md5_remote:
sum_matches = True sum_matches = True
if overwrite == 'always': if overwrite == 'always':
upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt) upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt, headers)
else: else:
get_download_url(module, s3, bucket, obj, expiry, changed=False) get_download_url(module, s3, bucket, obj, expiry, changed=False)
else: else:
sum_matches = False sum_matches = False
if overwrite in ('always', 'different'): if overwrite in ('always', 'different'):
upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt) upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt, headers)
else: else:
module.exit_json(msg="WARNING: Checksums do not match. Use overwrite parameter to force upload.") module.exit_json(msg="WARNING: Checksums do not match. Use overwrite parameter to force upload.")
# If neither exist (based on bucket existence), we can create both. # If neither exist (based on bucket existence), we can create both.
if bucketrtn is False and pathrtn is True: if bucketrtn is False and pathrtn is True:
create_bucket(module, s3, bucket, location) create_bucket(module, s3, bucket, location)
upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt) upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt, headers)
# If bucket exists but key doesn't, just upload. # If bucket exists but key doesn't, just upload.
if bucketrtn is True and pathrtn is True and keyrtn is False: if bucketrtn is True and pathrtn is True and keyrtn is False:
upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt) upload_s3file(module, s3, bucket, obj, src, expiry, metadata, encrypt, headers)
# Delete an object from a bucket, not the entire bucket # Delete an object from a bucket, not the entire bucket
if mode == 'delobj': if mode == 'delobj':