【问题标题】:move files between directories in single GCP bucket在单个 GCP 存储桶中的目录之间移动文件
【发布时间】:2020-01-07 20:12:27
【问题描述】:

我正在尝试设置云功能以在 GCP 中的一个存储桶内的文件夹之间移动文件。

每当用户将文件加载到提供的存储桶文件夹中时,我的云函数都会将文件移动到另一个大数据脚本正在处理的文件夹中。

在设置时显示成功,但是文件没有从源文件夹中移动。

感谢您的帮助

from google.cloud import storage

def move_file(bucket_name, bucket_Folder, blob_name):
    """Moves a blob from one folder to another with the same name."""
    bucket_name = 'bucketname'
    blob_name = 'filename'

    storage_client = storage.Client()

    bucket = storage_client.get_bucket(bucket_name)
    source_blob = bucket.blob("Folder1/" + blob_name)
    new_blob = bucket.copy_blob(source_blob, bucket, "Folder2/" + blob_name)
    blob.delete()

    print('Blob {} in bucket {} copied to blob {} .'.format(source_blob.name, bucket.name, new_blob.name))

【问题讨论】:

  • 你从哪里调用这个函数?请同时包含该代码。
  • 标签 [batch-file] 描述是 批处理文件是包含一系列命令的文本文件,这些命令由 MS-DOS、IBM OS/2 或Microsoft Windows 系统.
  • @DustinIngram 使用上述 py 脚本创建了一个云函数。所以我从它调用这个函数。
  • 您使用的是默认服务帐号还是自定义服务帐号,请查看权限详情
  • 我正在使用具有适当权限的自定义服务帐户,因为我的其他云功能正常工作。我想这与调用存储桶内的文件夹中的 py 脚本有关。此外,stakedrive 日志中也没有错误。

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


【解决方案1】:

根据您提供的代码,变量blob 未在任何地方定义,因此不会删除源文件。将该行改为source_blob.delete(),而不是blob.delete()

另外,我假设您知道您是 "moving" 只是一个文件。如果您想将所有以Folder1/ 为前缀的文件移动到Folder2,您可以这样做:

from google.cloud import storage

def move_files(self):

    storage_client = storage.Client()

    bucket = storage_client.get_bucket('bucketname')
    blobs = bucket.list_blobs(prefix='Folder1/')

    for blob in blobs:
     bucket.rename_blob(blob, new_name=blob.name.replace('Folder1/', 'Folder2/'))

对于后者,我认为可能有更有效或更好的方法来做到这一点。

【讨论】:

  • 太好了,感谢您的建议。我正在尝试将文件从 Folder1 移动到 folder2,就像文件进入时一样。顺便说一句,更改为 source_blob.delete() 不起作用。但是替换命令有效。
  • 嗨@Vicky Krish,如果Maxim 的回答对你有用,请接受他的回答。
【解决方案2】:

如果您只是在同一个存储桶内移动对象,您可以使用所需的路线 rename the object

在 Google Cloud Platform Storage 中没有文件夹,只是它们的错觉。存储桶名称之后的所有内容都是对象名称的一部分。

另外,我可以在您的函数中看到许多错误。您可以使用此通用函数将 blob 从一个文件夹移动到同一存储桶内的另一个文件夹:

从 google.cloud 导入存储 def rename_blob(bucket_name, blob_name, new_name): """重命名一个blob。""" # bucket_name = "你的桶名" # blob_name = "文件夹/我的对象" # new_name = "newfolder/myobject"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)

new_blob = bucket.rename_blob(blob, new_name)

print("Blob {} has been renamed to {}".format(blob.name, new_blob.name))

【讨论】:

  • 谢谢。它适用于重命名。但是我打算将文件夹保留在那里,以便在上传新文件时将文件从 Folder1 移动到 Folder2。
  • 您可以阅读有关 How Subdirectories Work 的文档,我发现使用 Google Cloud Storage 很有帮助。在这种情况下,即使您重命名文件夹的最后一个文件并且文件夹变空它也不会被删除,我只是做了一个测试并且文件夹仍然是空的,所以使用这种方法你很高兴
  • 谢谢你,克里斯。我运行了这个,它出现以下错误,错误:函数终止。建议的操作:检查日志以了解终止原因。详细信息:rename_blob() 缺少 1 个必需的位置参数:'new_name'
  • 该错误意味着您错过了一个参数。在此通道中: new_blob = bucket.rename_blob(blob, new_name) 您必须将值 'new_name' 替换为对象的新文件夹/名称 'newfolder/samename.csv' 或 'newfolder/anotherfolder/newname.csv'
  • 好吧,我做了同样的事情,但它给出了同样的错误。奇怪!
猜你喜欢
  • 2018-04-30
  • 2019-11-07
  • 2020-06-24
  • 1970-01-01
  • 2015-07-21
  • 2019-12-31
  • 2012-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多