Google cloud platform 如何在两个不同的gcp项目中自动复制文件?

Google cloud platform 如何在两个不同的gcp项目中自动复制文件?,google-cloud-platform,cloud,storage,bucket,Google Cloud Platform,Cloud,Storage,Bucket,事实上,我使用了这个命令,它工作得很好: gsutil cp gs:/bucket1/file.xml gs://bucket2/destination_folder (bucket1在GCP的项目1中,bucket2在GCP的另一个项目中) 但是我想每天早上9点做这个命令,我怎么能在我的GCP项目上以一种简单的方式做到这一点呢 编辑:它每天都会将文件从源bucket复制到目标bucket(两个bucket分别位于不同的项目中)。(实际上,当文件到达目标存储桶时,它会在bigquery中自动消

事实上,我使用了这个命令,它工作得很好:

gsutil cp gs:/bucket1/file.xml gs://bucket2/destination_folder
(bucket1在GCP的项目1中,bucket2在GCP的另一个项目中)

但是我想每天早上9点做这个命令,我怎么能在我的GCP项目上以一种简单的方式做到这一点呢

编辑:它每天都会将文件从源bucket复制到目标bucket(两个bucket分别位于不同的项目中)。(实际上,当文件到达目标存储桶时,它会在bigquery中自动消费和摄取,我只想触发我的命令gsutil并在每天早上停止手动执行)

(除了Data transfert的方法,因为我没有源项目的权限,所以无法激活Data transfert的服务帐户,我只有目标项目的权限。)

致以最良好的祝愿

实际上,我可以将一个文件从一个bucket复制到另一个bucket中,并将其复制到一个特定的文件夹中(RQ:这两个bucket位于同一个gcp项目中) 我不是来使用第二种方法和gs://

编辑2:

import base64
import  sys
import urllib.parse
# Imports the Google Cloud client library , dont forget the requirement or else it's ko
from google.cloud import storage


def copy_blob(
    bucket_name ="prod-data", blob_name="test.csv", destination_bucket_name = "prod-data-f", destination_blob_name ="channel_p"
):
    """Copies a blob from one bucket to another with a new name."""
    bucket_name = "prod-data"
    blob_name = "test.csv"
    destination_bucket_name = "prod-data-f"
    destination_blob_name = "channel_p/test.csv"

    storage_client = storage.Client()

    source_bucket = storage_client.bucket(bucket_name)
    source_blob = source_bucket.blob("huhu/"+blob_name)
    destination_bucket = storage_client.bucket(destination_bucket_name)

    blob_copy = source_bucket.copy_blob(
        source_blob, destination_bucket, destination_blob_name
    )

# Second Method (KO)
#
#   client = storage.Client()
#   with open('gs://prod-data-f/channelp.xml','wb') as file_obj:
#       client.download_blob_to_file(
#           'gs://pathsource/somefolder/channelp.xml', file_obj)
#
# End of second Method

    print(
        "Blob {} in bucket {} copied to blob {} in bucket {}.".format(
            source_blob.name,
            source_bucket.name,
            blob_copy.name,
            destination_bucket.name,
        )
    )

数据传输显然是实现这一点的正确工具,但由于您无法使用它,因此有其他解决方案


其中之一是使用云函数(您可以使用)复制文件,并在每天早上9点使用云函数触发。云功能也可以由发布/订阅消息触发。

我正在寻找的解决方案(它在我测试时对我有效):

Main.py

import base64
import os
import sys
import json
import uuid
import logging
from time import sleep
from flask import request
from random import uniform
from google.cloud import firestore
from google.cloud.exceptions import Forbidden, NotFound
from google.cloud import storage

# set retry deadline to 60s
DEFAULT_RETRY = storage.retry.DEFAULT_RETRY.with_deadline(60)

def Move2FinalBucket(data, context):

#    if 'data' in event:
#        name = base64.b64decode(event['data']).decode('utf-8')
#    else:
#        name = 'NO_DATA'
#        print('Message {}!'.format(name))


    # Get cache source bucket
    cache_bucket = storage.Client().get_bucket('nameofmysourcebucket', timeout=540, retry=DEFAULT_RETRY)

    # Get source file to copy
    blob2transfer = cache_bucket.blob('uu/oo/pp/filename.csv')

    # Get cache destination bucket
    destination_bucket = storage.Client().get_bucket('nameofmydestinationbucket', timeout=540, retry=DEFAULT_RETRY)

    # Get destination file
    new_file = destination_bucket.blob('kk/filename.csv')

    #rewrite into new_file
    new_file.rewrite(blob2transfer, timeout=540, retry=DEFAULT_RETRY)
requirement.txt

# Function dependencies, for example:
# package>=version
#google-cloud-storage==1.22.0
google-cloud-storage
google-cloud-firestore
google-api-core
flask==1.1.4
别忘了在这个CF上添加一个具有正确存储管理员的服务帐户,它会工作的


致以最诚挚的问候,

您是否使用您的用户凭证执行此
gsutil
操作?如果是这样,您是否还有一个服务帐户,可以在源项目中读取,然后在目标项目中写入?(我知道目标项目不是一个问题,你有权这样做)你的目标是“一次又一次地”将一个bucket复制到另一个bucket,还是在一个方向上“同步”两个bucket,还是镜像它们?用详细信息编辑您的问题。您好,当我执行命令时,我收到以下消息:“授权Cloud Shell bq正在请求您的凭据以进行GCP API调用。单击以授权此调用和未来需要您凭据的调用。”然后我单击“接受”,它将移动文件您的评论不会回答我的问题。您的问题状态为
gsutil
,您的评论状态为
bq
。用实际细节编辑您的问题。@JohnHanley,当我在gcp上的cloud shell上执行命令gsutil时,会出现此消息。当我第二次执行同一个命令时,它就不再出现了。我对我的帖子进行了一些精确的编辑。谢谢,这正是我所寻求的,我会尝试这个解决方案