found a way to upload a file from an url into s3 bucket but is there a way to direct it into a specific folder or create a folder?
I get it working to store into s3's root by using the code below
def save_image_s3(self, img_url, img_name):
"""Saves the image in img_url into S3 with the name img_name"""
conn = boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME)
k = Key(bucket)
k.key = img_name
fp = StringIO.StringIO(urllib2.urlopen(img_url).read())
k.set_contents_from_file(fp)
return img_name
Again, this does save it into my bucket's root directory but I am not sure how I can direct it to save into an existing folder or make a new folder if folder does not exist
Thanks in advance
You are very close, you can set k.key to the full "path" in the bucket to your object.
For example:
k.key = 'photos/%s' % img_name
This will save it to what you would consider a "folder" named photos. There is no need for the folder itself to be created first, since S3 folders do not actually exist but are implied by the appearance of one or more / appearing within object keys. Do not add / at the beginning.