簡體   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, 
    )

我正在使用此代碼創建 zip 文件並保存到 w3 存儲桶。 Fitst 我正在下載到本地系統,然后壓縮所有文件並將 zip 文件保存到 s3 存儲桶在我的本地系統中,我能夠提取 zip 文件,但是當我從 s3 存儲桶下載時,我無法提取它。

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

這是我上傳 zip 文件的 s3 路徑。 可能是什么原因請看一下

with塊之后移動上傳。

您正在關閉存檔之前上傳您的 zipfile。 ZipFile.close()

關閉存檔文件。 您必須在退出程序之前調用 close() 否則將不會寫入基本記錄。

closewith語句自動調用。

您在程序退出后打開本地文件 - 這意味着在 zipfile 關閉之后 - 這樣您的本地版本就不會損壞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM