簡體   English   中英

將 S3 存儲桶中的 gzip 文件提取到另一個 S3 存儲桶

[英]Extracting gzip file in an S3 bucket to another S3 Bucket

我試圖從一個 S3 存儲桶復制 gzip 文件,並使用 gzip 庫將其內容提取到另一個 S3 存儲桶。 我收到一個錯誤

不支持從末端查找

import boto3, json
from io import BytesIO
import gzip

def lambda_handler():
    try:
        s3 = boto3.resource('s3')
        copy_source = {
            'Bucket': 'srcbucket',
            'Key': 'samp.gz'
            }
        bucket = s3.Bucket('destbucket')
        bucketSrc = s3.Bucket('srcbucket')

        s3Client = boto3.client('s3', use_ssl=False)

        s3Client.upload_fileobj(                      # upload a new obj to s3
            Fileobj=gzip.GzipFile(              # read in the output of gzip -d
                None,                           # just return output as BytesIO
                'rb',                           # read binary
                fileobj=BytesIO(s3Client.get_object(Bucket='srcbucket', Key='samp.gz')['Body'].read())),
            Bucket='destbucket',                      # target bucket, writing to
            Key="")               # target key, writing to

    except Exception as e:
        print(e)

您無法解壓縮 ZIP 文件並按照您嘗試的方式上傳其組成文件。

您可以將整個 ZIP 文件解壓縮到/tmp Lambda 本地磁盤(注意這有 512MB 磁盤空間的限制),然后逐個文件上傳。 或者,如果它不適合磁盤,或者您不想保留在桌面上,那么您可以將 ZIP 文件的內容逐個文件地流式傳輸到內存中,然后將每個流上傳到 S3)。 在這兩種解決方案中,您都需要為每次上傳提供適當的密鑰。

暫無
暫無

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

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