简体   繁体   中英

Downloading a file from s3 to local machine using boto3 (python)

I'm trying to use a python script to automate downloading a file from AWS s3 to my local machine. The python script itself is hosted on ubuntu (AWS ec2 instance), so it's not recognizing a directory on my local machine.

Here's my code:

import os
import boto3
from boto3.session import Session
print("this script downloads the file from s3 to local machine")

s3 = boto3.resource('s3')

BUCKET_NAME = 'sfbucket.myBucket'
KEY = 'sf_events.json'

s3.Bucket(BUCKET_NAME).download_file(KEY, '/Users/Documents/my_file.json')

print('end')

However, this gives me the following error:

FileNotFoundError: [Errno 2] No such file or directory: '/Users/Documents/my_file.json.CDC5FEf4'

Can anyone tell me what I'm doing wrong? If I replace the output directory to /home/ubuntu/ it works fine, but I want the file on my local machine. Thanks in advance.

The script has to run on your local Windows machine not on EC2 instance.

Alternatively, you can simply use aws cli

aws s3 cp s3://sfbucket.myBucket/sf_events.json /Users/Documents/my_file.json

As you are trying to download the file using the EC2 instance, this machine doesn't know your local path /Users/Documents/my_file.json

You have two options:

  1. Run this script directly in your local machine. In this case, you have to run this script in your local machine and make sure you have access to this bucket.

  2. Run this script in EC2 Instance and copy. In this case, you have to download the file to somewhere /home/ubuntu/ and then after in your local machine make a copy from EC2. You can use [SCP][1]

You can install the SCP server in your local machine but the problem is that your local machine probably doesn't have a private IP, so every time that your local IP change you have to update the script. Maybe you should think about to do whatever you need to do with this file directly on EC2 Instance or if it's a real manual thing, sending via email, maybe?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM