简体   繁体   English

python无法提取上传到aws s3存储桶的zip文件

[英]python unable to extract zip file uploaded to aws s3 bucket

medias = ['https://baby-staging-bucket.s3.us-east-2.amazonaws.com/asset/0002.jpg', 
        'https://baby-staging-bucket.s3.us-east-2.amazonaws.com/asset/2.png', 
        'https://baby-staging-bucket.s3.us-east-2.amazonaws.com/asset/02.png'
    ]
for i in medias:
    file_name = i.split("/")[-1]
    urllib.urlretrieve (i, "media/"+file_name)

# writing files to a zipfile
local_os_path = f'media/{title}.zip'
with ZipFile(local_os_path, 'w') as zip:
    # writing each file one by one
    for file in medias:
        file_name = file.split("/")[-1]
        zip.write("media/"+file_name)
        os.remove("media/"+file_name)
    
    s3 = session.resource('s3')
    storage_path = f'asset/nfts/zip/{title}.zip'
    s3.meta.client.upload_file(Filename=local_os_path, Bucket=AWS_STORAGE_BUCKET_NAME, Key=storage_path)
    # os.remove(local_os_path)
    DesignerProduct.objects.filter(id=instance.id).update(
        zip_file_path=S3_BUCKET_URL + storage_path, 
    )

I am using this code to create zip file and saving to w3 bucket.我正在使用此代码创建 zip 文件并保存到 w3 存储桶。 Fitst i am downloading to localsystem then zipping all files and saving zip file to s3 bucket In my local system i am able to extract zip file but when i download from s3 bucket i am not able to extract it. Fitst 我正在下载到本地系统,然后压缩所有文件并将 zip 文件保存到 s3 存储桶在我的本地系统中,我能够提取 zip 文件,但是当我从 s3 存储桶下载时,我无法提取它。

https://baby-staging-bucket.s3.us-east-2.amazonaws.com/asset/nfts/zip/ant.zip

This is my path of s3 where zip file uploaded .这是我上传 zip 文件的 s3 路径。 what can be the reason please take a look可能是什么原因请看一下

Move the upload after the with block.with块之后移动上传。

You are uploading your zipfile before the archive is closed.您正在关闭存档之前上传您的 zipfile。 See ZipFile.close() :ZipFile.close()

Close the archive file.关闭存档文件。 You must call close() before exiting your program or essential records will not be written.您必须在退出程序之前调用 close() 否则将不会写入基本记录。

close is automatically called by the with statement. closewith语句自动调用。

You open your local file after the program exits - which means after the zipfile is closed - so your local version is not corrupted.您在程序退出后打开本地文件 - 这意味着在 zipfile 关闭之后 - 这样您的本地版本就不会损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM