为什么上传到AWS S3的文件大小为0B?

huangapple go评论46阅读模式
英文:

Why is the file uploaded to AWS S3 0B in size?

问题

以下是代码部分的中文翻译:

# 这是我的`save_picture`方法,用于打开图像文件并调整大小。我已经注释掉了保存方法,以避免将文件保存到磁盘,而是希望将其保存到S3。
def save_picture(object_id, form_picture, path):
    if form_picture is None:
        return None
    random_hex = token_hex(8)
    filename = form_picture.filename
    if '.' not in filename:
        return None
    extension = filename.rsplit('.', 1)[1].lower()
    if not allowed_file(extension, form_picture):
        return None
    picture_fn = f'{object_id}_{random_hex}.{extension}'
    picture_path = current_app.config['UPLOAD_FOLDER'] / path / picture_fn
    # 调整图像大小并保存小版本
    output_size = (1280, 720)
    i = Image.open(form_picture)
    i.thumbnail(output_size)
    # i.save(picture_path)
    return picture_fn

image_name = save_picture(object_id=new_object.id, form_picture=file, path=f'{object_type}_images')

s3 = boto3.client(
        's3',
        aws_access_key_id=current_app.config['AWS_ACCESS_KEY'],
        aws_secret_access_key=current_app.config['AWS_SECRET_ACCESS_KEY']
)

print(file)  # 这会打印出<FileStorage: 'Capture.JPG' ('image/jpeg')>,所以文件没问题

try:
    s3.upload_fileobj(
        file,
        current_app.config['AWS_BUCKET_NAME'],
        image_name,
        ExtraArgs={
            'ContentType': file.content_type
        }
    )
except Exception as e:
    print(e)
    return make_response({'msg': '出了点问题。'}, 500)

根据您的描述,您可以在S3中看到已上传的文件,但它显示为0 B的大小,而且如果尝试下载它,会显示无法查看。这可能是由于AWS配置或其他原因导致的上传失败。您提到尝试了不同的S3访问策略,但仍然遇到问题。请检查以下可能导致上传失败的问题:

  1. AWS密钥和访问权限:确保您的AWS密钥(AWS_ACCESS_KEYAWS_SECRET_ACCESS_KEY)具有适当的S3访问权限。您可能需要为其分配S3的写入权限。

  2. 文件对象:在upload_fileobj方法中,您应该传递一个文件对象而不是文件名。在您的代码中,file似乎是<FileStorage>对象,这可能不是boto3期望的文件对象。您可以尝试使用file.stream来传递文件对象。

  3. 错误处理:检查您的except部分是否正确处理了异常。您可以尝试输出异常的详细信息,以获取更多信息。

  4. 文件内容类型:确保file.content_type正确设置为图像的MIME类型。

  5. S3存储桶配置:检查S3存储桶的配置,确保它允许公共访问或适当的权限设置。您可以使用S3存储桶策略和访问控制清单来管理权限。

  6. 文件大小:确保文件不是空文件,并且在上传时包含有效的图像数据。

  7. 上传位置:验证图像是否被上传到S3的正确位置(存储桶和对象键)。

如果您排除了上述问题并且问题仍然存在,请提供更多详细信息,以便我可以提供更多帮助。

英文:

I am developing a webapplication with Flask as the backend and Nuxt JS as the frontend. I receive an image file from the frontend and can save it to my Flask directory structure locally. The file is ok and the images is being shown if I open it. Now i want to upload this image to AWS S3 instead of saving it to my disk. I use the boto3 SDK, here is my code:

Here is my save_picture method, that opens the image file and resizes it. I had the save method, but commented it out to avoid saving the file to disk as I want it only on S3.

def save_picture(object_id, form_picture, path):
    if form_picture is None:
        return None
    random_hex = token_hex(8)
    filename = form_picture.filename
    if &#39;.&#39; not in filename:
        return None
    extension = filename.rsplit(&#39;.&#39;, 1)[1].lower()
    if not allowed_file(extension, form_picture):
        return None
    picture_fn = f&#39;{object_id}_{random_hex}.{extension}&#39;
    picture_path = current_app.config[&#39;UPLOAD_FOLDER&#39;] / path / picture_fn
    # resizing image and saving the small version
    output_size = (1280, 720)
    i = Image.open(form_picture)
    i.thumbnail(output_size)
    # i.save(picture_path)
    return picture_fn
image_name = save_picture(object_id=new_object.id, form_picture=file, path=f&#39;{object_type}_images&#39;)

s3 = boto3.client(
        &#39;s3&#39;,
        aws_access_key_id=current_app.config[&#39;AWS_ACCESS_KEY&#39;],
        aws_secret_access_key=current_app.config[&#39;AWS_SECRET_ACCESS_KEY&#39;]
)

print(file)    # this prints &lt;FileStorage: &#39;Capture.JPG&#39; (&#39;image/jpeg&#39;)&gt;, so the file is ok

try:
    s3.upload_fileobj(
        file,
        current_app.config[&#39;AWS_BUCKET_NAME&#39;],
        image_name,
        ExtraArgs={
            &#39;ContentType&#39;: file.content_type
        }
    )
    except Exception as e:
        print(e)
        return make_response({&#39;msg&#39;: &#39;Something went wrong.&#39;}, 500)

I can see the uploaded file in my S3, but it shows 0 B in size and if I download it, it says that it cannot be viewed.

I have tried different access policies in S3, as well as many tutorials online, nothing seems to help. Changing the version of S3 to v3 when creating the client breaks the whole system and the file is not being uploaded at all with an access error.

What could be the reason for this upload failure? I it the config of AWS or something else?
Thank you!

答案1

得分: 2

感谢 @jarmod,我尝试避免图像处理,它有效。现在我正在调整图像大小,将其保存到磁盘,打开已保存的图像,而不是初始文件,然后将其发送到S3。然后,我会删除磁盘上的图像,因为我不需要它。

英文:

Thanks to @jarmod I tried to avoid the image processing and it worked. I am now resizing the image, saving it to disk, opening the saved image, not the initial file, and sending it to S3. I then delete the image on disk as I don't need it.

huangapple
  • 本文由 发表于 2023年2月14日 03:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定