无法使用Python脚本从AWS下载文件

时间:2018-03-09 09:35:47

标签: python-3.x amazon-s3

我正在尝试从AWS S3服务器下载文件到我的本地。但是,当我尝试运行以下查询时,我收到一条错误消息“IOError:[Errno 2]没有这样的文件或目录:”

import boto3
from botocore.client import Config

ACCESS_KEY_ID = '###'
ACCESS_SECRET_KEY = '###'
BUCKET_NAME = 'abc.helper'
FILE_NAME = 'k.png'


data = open(FILE_NAME, 'rb')

#### S3 Connect:
s3 = boto3.resource(
    's3',
    aws_access_key_id=ACCESS_KEY_ID,
    aws_secret_access_key=ACCESS_SECRET_KEY,
    config=Config(signature_version='s3v4')
)

#### Image download:
s3.Bucket(BUCKET_NAME).download_file(FILE_NAME, '/Users/kevin/desktop');

print ("Done")

我隐藏了访问密钥和密码ID,原因很明显。任何人都可以帮我找到错误的位置。感谢。

1 个答案:

答案 0 :(得分:0)

可能会帮助你

import boto3
import botocore

BUCKET_NAME = 'my-bucket' # replace with your bucket name
KEY = 'my_image_in_s3.jpg' # replace with your object key

s3 = boto3.resource('s3')

try:
    s3.Bucket(BUCKET_NAME).download_file(KEY, 'my_local_image.jpg')
except botocore.exceptions.ClientError as e:
    if e.response['Error']['Code'] == "404":
        print("The object does not exist.")
    else:
        raise

此链接可为您提供更多s3-example-download-file