【问题标题】:python unable to extract zip file uploaded to aws s3 bucketpython无法提取上传到aws s3存储桶的zip文件
【发布时间】:2021-11-14 16:34:11
【问题描述】:
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 路径。 可能是什么原因请看一下

【问题讨论】:

    标签: python amazon-s3


    【解决方案1】:

    将上传移到with 块之后。

    您在存档关闭之前上传您的 zip 文件。 见ZipFile.close()

    关闭存档文件。您必须在退出程序之前调用 close() 否则将不会写入基本记录。

    closewith 语句自动调用。

    您在程序退出后打开本地文件 - 这意味着在 zipfile 关闭后 - 因此您的本地版本没有损坏。

    【讨论】:

      猜你喜欢
      • 2020-01-18
      • 1970-01-01
      • 2017-10-17
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 2018-04-25
      • 2012-09-08
      • 2018-03-25
      相关资源
      最近更新 更多