【问题标题】:python - gzip string and upload to s3python - gzip 字符串并上传到 s3
【发布时间】:2021-06-22 02:45:59
【问题描述】:

我写了一个 sn-p 来从 s3 下载文件并修改一些 xml 数据,然后将其上传回 s3。数据是gzip,所以我先解压缩,然后修改并gzip回来。我看到 gzip 返回了一些数据(def 不是长度 0)为什么上传会这样?

    s3Key='test'
    try:
        bytes_buffer = io.BytesIO()
        s3.download_fileobj(Bucket=bucketName, Key=s3Key, Fileobj=bytes_buffer)
        byte_value = BytesIO(bytes_buffer.getvalue())
        gzipfile = GzipFile(fileobj=byte_value)
        content = gzipfile.read()
        xml = et.fromstring(content)
        for specialrequest in xml.xpath("(//*[local-name()='{}'])".format(nodeName)):
            # perform regex
            value = specialrequest.text
            value = 'test_replacement'
            specialrequest.text = value
        xml = et.tostring(xml)
        byte_value = StringIO()
        with GzipFile(fileobj=byte_value, mode="w") as f:
            f.write(xml)
        #s3.upload_fileobj(io.BytesIO(byte_value), bucketName, s3Key)
        response = s3.put_object(Body=byte_value.getvalue(), Bucket=bucketName, Key=s3Key)
        print(response)
    #print(byte_value.getvalue())
    except Exception:
        print "Unexpected error:", sys.exc_info()[0]
        pass

put成功但是内容长度总是0

{u'VersionId': 'mHZJAS6b2ordFx802D4egd56VFZjACOI', u'ETag': '"5d8fa27c1e14fee5d12c6856cc0c2074"', 'ResponseMetadata': {'HTTPStatusCode': 200, 'RetryAttempts': 0, 'HostId': 'Ig2nK1VtgURwGIHXXF8cgYqoUPrY/jW3ilhI8so9E9T0AKUn5Q3FX0IfrDsHanxqXS/4kO9Dje4=', 'RequestId': '1PY7DFWE37CACEM9', 'HTTPHeaders': {'content-length': '0', 'x-amz-id-2': 'Ig2nK1VtgURwGIHXXF8cgYqoUPrY/jW3ilhI8so9E9T0AKUn5Q3FX0IfrDsHanxqXS/4kO9Dje4=', 'server': 'AmazonS3', 'x-amz-request-id': '1PY7DFWE37CACEM9', 'etag': '"5d8fa27c1e14fee5d12c6856cc0c2074"', 'date': 'Tue, 22 Jun 2021 02:34:48 GMT', 'x-amz-version-id': 'mHZJAS6b2ordFx802D4egd56VFZjACOI'}}}

编辑:

改用 zlib 压缩后 - 我能够上传具有预期文件大小的文件(与下载的 gzip 相同),但是,当尝试在本地解压缩以验证数据时,它不断将其转换为 cpgz某种原因

xml = et.tostring(xml)
compressed = zlib.compress(str.encode(xml))
response = s3.put_object(Body=compressed, Bucket=bucketName, Key=s3Key)

【问题讨论】:

    标签: amazon-web-services python-2.7 amazon-s3 gzip


    【解决方案1】:

    假设 xml 与根对象相同,请尝试以下操作:

    import xml.etree.ElementTree as ET
    import boto3
    
    xml_string = ET.tostring(root, encoding='utf=8').decoding('utf8')
    print(xml_string) # Optional
    
    xml_byte = bytes(xml_string,'utf8')# gzip compress take bytes and not string
    
    gzip_compressed = gzip.compress(xml_byte)
    
    s3 = boto3.client('s3')
    response = s3.put_object(Body=gzip_compressed, Bucket=bucketName, Key=s3Key)
    
    if response:
          print("file uploaded successfully") 
    

    【讨论】:

      猜你喜欢
      • 2011-02-17
      • 2019-06-13
      • 2017-05-22
      • 2021-07-22
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      相关资源
      最近更新 更多