【问题标题】:python google cloud function to untar filespython 谷歌云功能解压文件
【发布时间】:2019-03-16 17:22:32
【问题描述】:

我是 GCP 的新手,有使用 Python 的经验。 我尝试为一个场景编写一个云函数来解压 GCS 中的文件并将它们复制到另一个存储桶。

from google.cloud import storage
import tarfile

client = storage.Client()

def untar_lookupfiles(data, context):
    # Get the file that has been uploaded to GCS
    bucket = client.get_bucket(data['Source_bucketName'])

    #copy the tarfiles to another bucket
    bucket = client.get_bucket('Target_bucketName')
    blob = bucket.blob('gs://path/to/file.name')
    blob.upload_from_filename('/path/to/source.file')

    # Untar the files
    print('Untaring Files: {}'.format(data['name']))
    untar = tarfile.open("marfiles.tar.gz", "r:gz") # filename is hard coded should be replaced with data['name']
    untar.extractall(path=dir)

但看起来这段代码中缺少一些东西,有人可以帮我写代码吗?我没有使用 nodejs 编写代码的经验。感谢您的帮助。

【问题讨论】:

  • 你遇到了什么错误?
  • 在你用数据填充 tarfiles 变量后,我看不到任何地方使用它。你的意思是tarfile
  • @Brandon,我重用了其他东西的代码,现在将其删除,我的要求是将 tar 文件复制到新存储桶中并解压缩它们。
  • 现在你没有使用blob 变量来保存get_blob 的结果。
  • @BrandonYarbrough,我删除了 get_blob 行,

标签: python google-cloud-platform google-cloud-storage google-cloud-functions


【解决方案1】:

这是一个函数,它将解压放在一个存储桶中的文件并将内容放入另一个存储桶:

requirements.txt:

google-cloud-storage

main.py:

import io
import os
import tarfile

from google.cloud import storage

client = storage.Client()
input_bucket = client.get_bucket('INPUT-BUCKET-NAME')
output_bucket = client.get_bucket('OUTPUT-BUCKET-NAME')


def untar(data, context):
    # Get the contents of the uploaded file
    input_blob = input_bucket.get_blob(data['name']).download_as_string()

    # Turn the upload file into a tar file
    tar = tarfile.open(fileobj=io.BytesIO(input_blob))

    # Iterate over all files in the tar file
    for member in tar.getnames():

        # Extract the individual file
        file_object = tar.extractfile(member)

        # Check if it's a file or directory (which should be skipped)
        if file_object:

            # Create a new blob instance in the output bucket
            output_blob = output_bucket.blob(os.path.join(data['name'], member))

            # Write the contents of the file to the output blob
            output_blob.upload_from_string(file_object.read())

部署:

$ gcloud beta functions deploy test \
    --runtime python37 \
    --project PROJECT_NAME \
    --trigger-resource INPUT_BUCKET_NAME \
    --trigger-event google.storage.object.finalize

【讨论】:

  • 非常感谢达斯汀,代码工作起来很迷人,我正在尝试检查文件是否以 tar.gz 结尾,因为多个文件同时落在输入存储桶中。让我自己想办法,再次感谢您
  • 我尝试重用您的代码并编写了以下代码来解压缩文件并复制到另一个存储桶。如果我错了,你能纠正我吗def untar(data, context): # Get the contents of the uploaded file input_blob = input_bucket.get_blob(data['name']).download_as_string() # check filename endswith "csv.gz" if data['name'].endswith('csv.gz'): gz = gzip.open(fileobj=io.BytesIO(input_blob)) output_blob = output_bucket.blob(os.path.join(data['name'], gz)) output_blob.upload_from_string(file_object.read())
  • 您不需要使用gziptarfile.open 命令采用mode 参数,您也可以将其设置为r:gz 以解压缩文件。您需要根据文件类型选择性地设置它。见docs.python.org/3/library/tarfile.html#tarfile.open
  • 我尝试使用以下代码解压缩 Mar_Test.csv.gz 文件,但它抛出错误tar = tarfile.open(fileobj=io.BytesIO(input_blob),mode='r:gz') 错误消息:ValueError: invalid literal for int( ) 以 8 为基数:',2014\nMa'
  • .csv.gz 文件不是 tar 文件,而是压缩的 CSV 文件。
猜你喜欢
  • 2017-11-14
  • 2021-08-10
  • 2019-05-14
  • 2020-09-03
  • 2021-10-12
  • 2019-12-03
  • 2018-12-11
  • 1970-01-01
  • 2019-01-19
相关资源
最近更新 更多