首页 > 解决方案 > 将文件上传到 s3 并取回公共 URL

问题描述

我正在尝试在 s3 Amazon 上使用 boto3 Python 上传 JSON 文件。文件上传成功,但我没有收到返回的公共 URL。

这是我的代码:

import boto3
from botocore.exceptions import NoCredentialsError
from credential import ACCESS_KEY , SECRET_KEY



def upload_to_aws(local_file, bucket, s3_file):
    s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
                      aws_secret_access_key=SECRET_KEY)

    try:
        s3.upload_file(local_file, bucket, s3_file)
        print("Upload Successful")
        return True
    except FileNotFoundError:
        print("The file was not found")
        return False
    except NoCredentialsError:
        print("Credentials not available")
        return False


uploaded = upload_to_aws('temp_file.json', 'feed-crawler', '0output_test.json')

标签: pythonamazon-s3

解决方案


推荐阅读