如何在将文件存储到 GCS 存储桶时重命名文件 [英] How to rename a file while storing it to a GCS bucket

查看:30
本文介绍了如何在将文件存储到 GCS 存储桶时重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 directory.post 验证之一读取文件,我需要上传一个附加时间戳的文件.

I am reading a file from one of directory.post validations I need to upload a file with appending timestamp to it.

如何在将文件上传到 GCS 存储桶时重命名文件?

How do I rename the file while uploading it to a GCS bucket?

我使用的是 Google Storage 客户端.我看到对于 S3,boto 客户端有一种方法,我们可以在其中传递 upload_name= 参数,但我在 GCS 中没有看到类似的方法.

I am using the Google Storage client. I see that for S3 the boto client has a method where we can pass a upload_name= parameter but I don't see a similar one in GCS.

推荐答案

您可以使用 rename_blob 来达到相同的效果.

You could use rename_blob to achieve the same result.

from google.cloud import storage

storage_client = storage.Client() 
bucket = storage_client.get_bucket("mybucket")
blob = bucket.blob("myfile")
blob.upload_from_filename("mynewfile")
bucket.rename_blob(blob, "mynewfile")

另一种选择是使用 rest API可以只传递name参数.

Another alternative is using the rest API where you can just pass the name parameter.

这篇关于如何在将文件存储到 GCS 存储桶时重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆