将文件从URL复制到s3并将其公开

时间:2016-03-09 13:28:15

标签: python django amazon-s3 boto3

aws_session = Session(
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
region_name=settings.AWS_DEFAULT_LAMBDA_REGION
)

s3_resource = aws_session.resource('s3')

def copy_audio_file(source_audio_url):
    url_validator = URLValidator()
    try:
        url_validator(source_audio_url)
    except ValidationError as e:
        raise CopyAudioFileException("Got error while trying to copy audio file. URL: {0}, Error: {1}".
                                 format(source_audio_url, e.message))

    filename = get_random_string(32) + ".mp3"
    destination_audio_url = settings.AWS_BASE_URL_S3 + settings.AWS_BUCKET_CALL_RECORDINGS + '/' + filename

    try:
        s3_resource.Object(settings.AWS_BUCKET_CALL_RECORDINGS, filename).copy_from(
        CopySource=source_audio_url)
    except ClientError as e:
        logger.exception(e)
        raise CopyAudioFileException("Got error while trying to copy audio file. URL: {0}, Error: {1}".
                                 format(source_audio_url, e.message))
return destination_audio_url

我使用上面的函数将文件从url复制到s3。我想公开这个文件。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您需要在ACL='public-read'方法中传递copy_from

s3_resource.Object(settings.AWS_BUCKET_CALL_RECORDINGS,filename) \
    .copy_from(CopySource=source_audio_url, ACL='public-read')