【问题标题】:Negative seek in write mode写模式下的负寻道
【发布时间】:2021-12-07 09:13:58
【问题描述】:

我从 S3 下载了一个压缩文件并尝试解压缩:

with zipfile.ZipFile('/tmp/DataPump_10000838.zip', 'r') as zip_ref:
    testList = []
    for i in zip_ref.namelist():
        if (i.startswith("__MACOSX/") == False):
            val = '/tmp/'+i
            testList.append(val)

现在,我的/tmp 文件夹中有一些解压缩文件。我想打开这些文件,gzip 并移动到 S3 存储桶。解压文件的路径在testList

s3_filename = 'sample'

s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket('testunzipping')    
    
    for i in testList:
        with contextlib.ExitStack() as stack:
            source_file = stack.enter_context(open(i , mode="rb"))
            destination_file = io.BytesIO()
            destination_file_gz = stack.enter_context(gzip.GzipFile(fileobj=destination_file, mode='wb'))
            while True:
                chunk = source_file.read(1024)
                if not chunk:
                    break
                destination_file_gz.write(chunk)
            destination_file_gz.seek(0)
            
            bucket.upload_fileobj(destination_file_gz, fileName)

我正在尝试这个,但我得到一个错误:

Response
{
  "errorMessage": "Negative seek in write mode",
  "errorType": "OSError",
  "requestId": "",
  "stackTrace": [
    "  File \"/var/lang/lib/python3.9/importlib/__init__.py\", line 127, in import_module\n    return _bootstrap._gcd_import(name[level:], package, level)\n",
    "  File \"<frozen importlib._bootstrap>\", line 1030, in _gcd_import\n",
    "  File \"<frozen importlib._bootstrap>\", line 1007, in _find_and_load\n",
    "  File \"<frozen importlib._bootstrap>\", line 986, in _find_and_load_unlocked\n",
    "  File \"<frozen importlib._bootstrap>\", line 680, in _load_unlocked\n",
    "  File \"<frozen importlib._bootstrap_external>\", line 850, in exec_module\n",
    "  File \"<frozen importlib._bootstrap>\", line 228, in _call_with_frames_removed\n",
    "  File \"/var/task/lambda_function.py\", line 51, in <module>\n    destination_file_gz.seek(0)\n",
    "  File \"/var/lang/lib/python3.9/gzip.py\", line 384, in seek\n    raise OSError('Negative seek in write mode')\n"
  ]
}

我该如何解决这个问题?

【问题讨论】:

    标签: python amazon-s3 gzip file-handling oserror


    【解决方案1】:

    Python 的 gzip 包装器不支持查找。不过,这可能并不重要,因为您要上传 gzip 的字节,而不是在 gzip 视图上查找所产生的解压缩字节。此外,您需要close gzip 文件,因为它不知道您已完成写入,并且可能包含要刷新的最终缓冲区:

        # Ensure the gzip wrapper is done writing its data
        destination_file_gz.close()
    
        # Seek to the start of the raw data, not the gzip'd version
        destination_file.seek(0)
        # And upload that raw data    
        bucket.upload_fileobj(destination_file, fileName)
    

    【讨论】:

    • 然后我会得到一个"I/O operation on closed file.",error :(
    • @x89 啊,是的,您在使用之前没有关闭 gz 文件,请参阅我的编辑。
    • 我看到文件现在是在 S3 中创建的。但是,未检测到文件类型 (-)。另外,当我下载文件并打开它时,它只是奇怪的字符。我如何知道它是否已成功压缩?
    • 如果您需要设置内容类型,请在您的上传调用中添加ContentType='application/gzip'。您应该能够使用 gzip 阅读器打开该文件。我会用你想读取这些文件的任何组件对其进行测试。
    • 你的意思是这样bucket.upload_fileobj(destination_file, fileName, ContentType='application/gzip')这会抛出bucket_upload_fileobj() got an unexpected keyword argument 'ContentType
    猜你喜欢
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 2022-01-17
    • 2018-05-31
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多