简体   繁体   English

从 Google Cloud Function (Python) 将新文件写入 Google Cloud Storage 存储桶

[英]Writing a new file to a Google Cloud Storage bucket from a Google Cloud Function (Python)

I am trying to write a new file (not upload an existing file) to a Google Cloud Storage bucket from inside a Python Google Cloud Function.我正在尝试从 Python Google Cloud Function 内部将新文件(而不是上传现有文件)写入 Google Cloud Storage 存储桶。

Any ideas would be much appreciated.任何想法将不胜感激。

Thanks.谢谢。

You have to create your file locally and then to push it to GCS.您必须在本地创建文件,然后将其推送到 GCS。 You can't create a file dynamically in GCS by using open.您不能使用 open 在 GCS 中动态创建文件。

For this, you can write in the /tmp directory which is an in memory file system.为此,您可以在/tmp目录中写入内存文件系统。 By the way, you will never be able to create a file bigger than the amount of the memory allowed to your function minus the memory footprint of your code.顺便说一句,您将永远无法创建大于函数允许的内存量减去代码的内存占用量的文件。 With a function with 2Gb, you can expect a max file size of about 1.5Gb.使用 2Gb 的函数,您可以预期最大文件大小约为 1.5Gb。

Note: GCS is not a file system, and you don't have to use it like this注意:GCS 不是文件系统,你不必像这样使用它

 from google.cloud import storage
 import io

 # bucket name
 bucket = "my_bucket_name"

 # Get the bucket that the file will be uploaded to.
 storage_client = storage.Client()
 bucket = storage_client.get_bucket(bucket)

 # Create a new blob and upload the file's content.
 my_file = bucket.blob('media/teste_file01.txt')

 # create in memory file
 output = io.StringIO("This is a test \n")

 # upload from string
 my_file.upload_from_string(output.read(), content_type="text/plain")

 output.close()

 # list created files
 blobs = storage_client.list_blobs(bucket)
 for blob in blobs:
     print(blob.name)

# Make the blob publicly viewable.
my_file.make_public()

You can now write files directly to Google Cloud Storage.您现在可以将文件直接写入 Google Cloud Storage。 It is no longer necessary to create a file locally and then upload it.不再需要在本地创建文件然后上传。

You can use the blob.open() as follows:您可以使用 blob.open() 如下:


from google.cloud import storage
    
def write_file():
    client = storage.Client()
        bucket = client.get_bucket('bucket-name')
        blob = bucket.blob('path/to/new-blob-name.txt') 
        ## Use bucket.get_blob('path/to/existing-blob-name.txt') to write to existing blobs
        with blob.open(mode='w') as f:
            for line in object: 
                f.write(line)

You can find more examples and snippets here: https://github.com/googleapis/python-storage/tree/main/samples/snippets您可以在此处找到更多示例和片段: https : //github.com/googleapis/python-storage/tree/main/samples/snippets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Google Cloud Function 将文件从 Cloud Storage 存储桶推送到实例中? - How can I use a Google Cloud Function to push a file from a Cloud Storage bucket into an instance? Python3 中的 Cloud Function - 从 Google Cloud Bucket 复制到另一个 Google Cloud Bucket - Cloud Function in Python3 - copy from Google Cloud Bucket to another Google Cloud Bucket 使用 Python 将文件上传到 Google Cloud Storage Bucket 子目录 - Upload File to Google Cloud Storage Bucket Sub Directory using Python 谷歌云 function - Python - 将存储桶中的文件解压缩到同一个存储桶 - Google cloud function - Python - unzip a file that is in a bucket to the same bucket 将文件从 /tmp 文件夹移动到 Google Cloud Storage 存储桶 - Move file from /tmp folder to Google Cloud Storage bucket 如何从谷歌云存储桶中读取 python 代码中的.json 文件 - How to Read .json file in python code from google cloud storage bucket 使用 python 获取某个文件后,如何从 Google 云存储桶中获取文件? - How do you fetch files from Google cloud storage bucket after a certain file is fetched using python? 使用python将数据写入谷歌云存储 - Writing data to google cloud storage using python 从谷歌云存储桶中直接读取 netCDF 文件到 python - Reading netCDF file from google cloud bucket directly into python python和谷歌云存储 - python and google cloud storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM