Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download S3 Files with Boto

I am trying to set up an app where users can download their files stored in an S3 Bucket. I am able to set up my bucket, and get the correct file, but it won't download, giving me the this error: No such file or directory: 'media/user_1/imageName.jpg' Any idea why? This seems like a relatively easy problem, but I can't quite seem to get it. I can delete an image properly, so it is able to identify the correct image.

Here's my views.py

def download(request, project_id=None):
    conn = S3Connection('AWS_BUCKET_KEY', 'AWS_SECRET_KEY')
    b = Bucket(conn, 'BUCKET_NAME')
    k = Key(b)
    instance = get_object_or_404(Project, id=project_id)
    k.key = 'media/'+str(instance.image)
    k.get_contents_to_filename(str(k.key))
    return redirect("/dashboard/")
like image 471
pepper5319 Avatar asked May 25 '16 16:05

pepper5319


People also ask

How do I download an entire S3 bucket?

To download an entire bucket to your local file system, use the AWS CLI sync command, passing it the s3 bucket as a source and a directory on your file system as a destination, e.g. aws s3 sync s3://YOUR_BUCKET . . The sync command recursively copies the contents of the source to the destination.

How do I download from S3 bucket to local using command line?

Your answer You can use cp to copy the files from an s3 bucket to your local system. Use the following command: $ aws s3 cp s3://bucket/folder/file.txt . To know more about AWS S3 and its features in detail check this out!


1 Answers

The problem is that you are downloading to a local directory that doesn't exist (media/user1). You need to either:

  • Create the directory on the local machine first
  • Just use the filename rather than a full path
  • Use the full path, but replace slashes (/) with another character -- this will ensure uniqueness of filename without having to create directories

The last option could be achieved via:

k.get_contents_to_filename(str(k.key).replace('/', '_'))

See also: Boto3 to download all files from a S3 Bucket

like image 129
John Rotenstein Avatar answered Nov 10 '22 11:11

John Rotenstein