2016-03-09 71 views
0
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。我想公開這個文件。有什麼建議麼?將文件複製到s3從一個網址,並公開

+0

目前尚不清楚問題是什麼。對於代碼審查,您可以嘗試https://codereview.stackexchange.com/ –

+0

我已成功複製該文件。現在我想讓tom將文件的權限更改爲public。 – Simar

回答

1

您需要通過ACL='public-read',copy_from方法。

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