当我尝试在s3上上传文件时,它显示为ClientError:ClientEr ... Denied',)

时间:2019-01-14 16:07:44

标签: django-rest-framework boto3

此处file_name ='imgs_ / 1_image004.jpg'。
当我尝试调用该函数时,它在行上显示错误

bucket.put_object(Key=file_name, Body=f_content, ACL='public-read')

和错误类似

  

ClientError:调用时发生错误(AccessDenied)   PutObject操作:访问被拒绝。

我的代码是:

 def upload_image_to_s3(file_name, f_content):

    import ipdb;
    ipdb.set_trace();
    #
    s3 = boto3.resource('s3', aws_access_key_id=conf.AWS_ACCESS_KEY,
                            aws_secret_access_key=conf.AWS_SECRET_ACCESS_KEY,
                            region_name=conf.REGION)
    #
    #
    BUCKET_NAME = conf.BUCKET_NAME
    PATH = conf.IMAGE_UPLOAD_PATH
    REGION = conf.REGION
    bucket = s3.Bucket(BUCKET_NAME)
    exists = True

    try:
        s3.meta.client.head_bucket(Bucket=BUCKET_NAME)
    except botocore.exceptions.ClientError as e:
        # If a client error is thrown, then check that it was a 404 error.
        # If it was a 404 error, then the bucket does not exist.
        error_code = int(e.response['Error']['Code'])
        if error_code == 404:
            exists = False

    bucket.put_object(Key=file_name, Body=f_content, ACL='public-read')
    s3_path = "https://" + REGION + ".amazonaws.com/" + BUCKET_NAME + "/" + PATH + "/" + file_name
    return s3_path

0 个答案:

没有答案