Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
is there a way to store a file from url into s3's specific folder, not just into the root?

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!