Storage 移轉服務用戶端程式庫

本頁面說明如何開始使用 Google Storage Transfer API 適用的 Cloud 用戶端程式庫。如要進一步瞭解 Cloud API 適用的用戶端程式庫 (包括舊版 Google API 用戶端程式庫),請參閱用戶端程式庫說明

如要瞭解如何從 Google API 用戶端程式庫更新至本頁所述的 Cloud 用戶端程式庫,請參閱 Storage 移轉服務遷移指南

安裝用戶端程式庫

C++

如要進一步瞭解如何安裝 C++ 程式庫,請參閱「設定 C++ 開發環境」指南。

C#

詳情請參閱「設定 C# 開發環境」。

如果您使用的是 Visual Studio 2017 以上版本,請開啟 NuGet 套件管理器視窗,然後輸入以下內容:

Install-Package Google.Cloud.StorageTransfer.V1

如果您使用 .NET Core 指令列介面工具安裝依附元件,請執行下列指令:

dotnet add package Google.Cloud.StorageTransfer.V1

Go

詳情請參閱「設定 Go 開發環境」。

go get cloud.google.com/go/storagetransfer

Java

詳情請參閱「設定 Java 開發環境」一文。

如果您使用 Maven,請在 pom.xml 檔案中加入以下指令:

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-storage-transfer</artifactId>
    <version>0.2.3</version>
</dependency>

如果您使用無 BoM 的 Gradle,請在依附元件中加入以下指令:

implementation 'com.google.cloud:google-cloud-storage-transfer:0.2.3'

Node.js

詳情請參閱「設定 Node.js 開發環境」一文。

npm install @google-cloud/storage-transfer

PHP

詳情請參閱「在 Google Cloud 上使用 PHP」。

composer require google/cloud

Python

詳情請參閱「設定 Python 開發環境」一文。

pip install --upgrade google-cloud-storage-transfer

Ruby

詳情請參閱「設定 Ruby 開發環境」一文。

gem install google-cloud-storage_transfer

設定驗證

使用用戶端程式庫時,您會使用應用程式預設憑證進行驗證。詳情請參閱「使用用戶端程式庫進行驗證」。

使用用戶端程式庫

以下範例說明如何使用用戶端程式庫。

在使用這些範例之前,請按照「設定存取權」中的操作說明,設定必要的權限。

C++

如要瞭解如何安裝及使用 Storage 移轉服務用戶端程式庫,請參閱 Storage 移轉服務用戶端程式庫

如要向 Storage 移轉服務進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


#include "google/cloud/storagetransfer/v1/storage_transfer_client.h"
#include <iostream>
#include <string>

int main(int argc, char* argv[]) try {
  if (argc != 2) {
    std::cerr << "Usage: " << argv[0] << " project-id\n";
    return 1;
  }

  namespace storagetransfer = ::google::cloud::storagetransfer_v1;
  auto client = storagetransfer::StorageTransferServiceClient(
      storagetransfer::MakeStorageTransferServiceConnection());

  ::google::storagetransfer::v1::ListTransferJobsRequest request;
  request.set_filter(R"""({"projectId": ")""" + std::string{argv[1]} + "\"}");
  for (auto r : client.ListTransferJobs(request)) {
    if (!r) throw std::move(r).status();
    std::cout << r->DebugString() << "\n";
  }

  return 0;
} catch (google::cloud::Status const& status) {
  std::cerr << "google::cloud::Status thrown: " << status << "\n";
  return 1;
}

C#

如要瞭解如何安裝及使用 Storage 移轉服務用戶端程式庫,請參閱 Storage 移轉服務用戶端程式庫。詳情請參閱 Storage 移轉服務 C# API 參考說明文件

如要向 Storage 移轉服務進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

using System;
using Google.Cloud.StorageTransfer.V1;

namespace StorageTransfer.Samples
{
    public class QuickstartSample
    {
        public TransferJob Quickstart(
            // Your Google Cloud Project ID
            string projectId = "my-project-id",
            // The GCS bucket to transfer data from
            string sourceBucket = "my-source-bucket",
            // The GCS bucket to transfer data to
            string sinkBucket = "my-sink-bucket")
        {
            TransferJob transferJob = new TransferJob
            {
                ProjectId = projectId,
                TransferSpec = new TransferSpec
                {
                    GcsDataSink = new GcsData { BucketName = sourceBucket },
                    GcsDataSource = new GcsData { BucketName = sinkBucket }
                },
                Status = TransferJob.Types.Status.Enabled
            };

            StorageTransferServiceClient client = StorageTransferServiceClient.Create();
            TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob });
            client.RunTransferJob(new RunTransferJobRequest
            {
                JobName = response.Name,
                ProjectId = projectId
            });

            Console.WriteLine($"Created and ran transfer job from {sourceBucket} to {sinkBucket} with name {response.Name}");

            return response;
        }
    }
}

Go

如要瞭解如何安裝及使用 Storage 移轉服務用戶端程式庫,請參閱 Storage 移轉服務用戶端程式庫。詳情請參閱 Storage 移轉服務 Go API 參考說明文件

如要向 Storage 移轉服務進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

import (
	"context"
	"fmt"
	"io"

	storagetransfer "cloud.google.com/go/storagetransfer/apiv1"
	"cloud.google.com/go/storagetransfer/apiv1/storagetransferpb"
)

// quickstart creates and runs a transfer job between two GCS buckets.
func quickstart(w io.Writer, projectID string, sourceGCSBucket string, sinkGCSBucket string) (*storagetransferpb.TransferJob, error) {
	// Your Google Cloud Project ID
	// projectID := "my-project-id"

	// The name of the GCS bucket to transfer data from
	// sourceGCSBucket := "my-source-bucket"

	// The name of the GCS bucket to transfer data to
	// sinkGCSBucket := "my-sink-bucket"
	ctx := context.Background()
	client, err := storagetransfer.NewClient(ctx)
	if err != nil {
		return nil, fmt.Errorf("storagetransfer.NewClient: %w", err)
	}
	defer client.Close()

	req := &storagetransferpb.CreateTransferJobRequest{
		TransferJob: &storagetransferpb.TransferJob{
			ProjectId: projectID,
			TransferSpec: &storagetransferpb.TransferSpec{
				DataSource: &storagetransferpb.TransferSpec_GcsDataSource{
					GcsDataSource: &storagetransferpb.GcsData{BucketName: sourceGCSBucket}},
				DataSink: &storagetransferpb.TransferSpec_GcsDataSink{
					GcsDataSink: &storagetransferpb.GcsData{BucketName: sinkGCSBucket}},
			},
			Status: storagetransferpb.TransferJob_ENABLED,
		},
	}
	resp, err := client.CreateTransferJob(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("failed to create transfer job: %w", err)
	}
	if _, err = client.RunTransferJob(ctx, &storagetransferpb.RunTransferJobRequest{
		ProjectId: projectID,
		JobName:   resp.Name,
	}); err != nil {
		return nil, fmt.Errorf("failed to run transfer job: %w", err)
	}

	fmt.Fprintf(w, "Created and ran transfer job from %v to %v with name %v", sourceGCSBucket, sinkGCSBucket, resp.Name)
	return resp, nil
}

Java

如要瞭解如何安裝及使用 Storage 移轉服務用戶端程式庫,請參閱 Storage 移轉服務用戶端程式庫。詳情請參閱 Storage 移轉服務 Java API 參考說明文件

如要向 Storage 移轉服務進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

import com.google.storagetransfer.v1.proto.StorageTransferServiceClient;
import com.google.storagetransfer.v1.proto.TransferProto.CreateTransferJobRequest;
import com.google.storagetransfer.v1.proto.TransferProto.RunTransferJobRequest;
import com.google.storagetransfer.v1.proto.TransferTypes.GcsData;
import com.google.storagetransfer.v1.proto.TransferTypes.TransferJob;
import com.google.storagetransfer.v1.proto.TransferTypes.TransferSpec;

public class QuickstartSample {
  /** Quickstart sample using transfer service to transfer from one GCS bucket to another. */
  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.

    // Your Google Cloud Project ID
    String projectId = "your-project-id";

    // The name of the source GCS bucket to transfer objects from
    String gcsSourceBucket = "your-source-gcs-source-bucket";

    // The name of the  GCS bucket to transfer  objects to
    String gcsSinkBucket = "your-sink-gcs-bucket";

    quickStartSample(projectId, gcsSourceBucket, gcsSinkBucket);
  }

  public static void quickStartSample(
      String projectId, String gcsSourceBucket, String gcsSinkBucket) throws Exception {

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources,
    // or use "try-with-close" statement to do this automatically.
    try (StorageTransferServiceClient storageTransfer = StorageTransferServiceClient.create()) {

      TransferJob transferJob =
          TransferJob.newBuilder()
              .setProjectId(projectId)
              .setTransferSpec(
                  TransferSpec.newBuilder()
                      .setGcsDataSource(GcsData.newBuilder().setBucketName(gcsSourceBucket))
                      .setGcsDataSink(GcsData.newBuilder().setBucketName(gcsSinkBucket)))
              .setStatus(TransferJob.Status.ENABLED)
              .build();

      TransferJob response =
          storageTransfer.createTransferJob(
              CreateTransferJobRequest.newBuilder().setTransferJob(transferJob).build());

      storageTransfer
          .runTransferJobAsync(
              RunTransferJobRequest.newBuilder()
                  .setProjectId(projectId)
                  .setJobName(response.getName())
                  .build())
          .get();
      System.out.println(
          "Created and ran transfer job between two GCS buckets with name " + response.getName());
    }
  }
}

Node.js

如要瞭解如何安裝及使用 Storage 移轉服務用戶端程式庫,請參閱 Storage 移轉服務用戶端程式庫。詳情請參閱 Storage 移轉服務 Node.js API 參考說明文件

如要向 Storage 移轉服務進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


// Imports the Google Cloud client library
const {
  StorageTransferServiceClient,
} = require('@google-cloud/storage-transfer');

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// Your project id
// const projectId = 'my-project'

// The ID of the GCS bucket to transfer data from
// const gcsSourceBucket = 'my-source-bucket'

// The ID of the GCS bucket to transfer data to
// const gcsSinkBucket = 'my-sink-bucket'

// Creates a client
const client = new StorageTransferServiceClient();

/**
 * Creates a one-time transfer job.
 */
async function quickstart() {
  // Creates a request to transfer from the source bucket to
  // the sink bucket
  const createRequest = {
    transferJob: {
      projectId: projectId,
      transferSpec: {
        gcsDataSource: {bucketName: gcsSourceBucket},
        gcsDataSink: {bucketName: gcsSinkBucket},
      },
      status: 'ENABLED',
    },
  };

  // Runs the request and creates the job
  const [transferJob] = await client.createTransferJob(createRequest);

  const runRequest = {
    jobName: transferJob.name,
    projectId: projectId,
  };
  await client.runTransferJob(runRequest);

  console.log(
    `Created and ran a transfer job from ${gcsSourceBucket} to ${gcsSinkBucket} with name ${transferJob.name}`
  );
}

quickstart();

PHP

如要瞭解如何安裝及使用 Storage 移轉服務用戶端程式庫,請參閱 Storage 移轉服務用戶端程式庫。詳情請參閱 Storage 移轉服務 PHP API 參考說明文件

如要向 Storage 移轉服務進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

use Google\Cloud\StorageTransfer\V1\Client\StorageTransferServiceClient;
use Google\Cloud\StorageTransfer\V1\CreateTransferJobRequest;
use Google\Cloud\StorageTransfer\V1\GcsData;
use Google\Cloud\StorageTransfer\V1\RunTransferJobRequest;
use Google\Cloud\StorageTransfer\V1\TransferJob;
use Google\Cloud\StorageTransfer\V1\TransferJob\Status;
use Google\Cloud\StorageTransfer\V1\TransferSpec;

/**
 * Creates and runs a transfer job between two GCS buckets
 *
 * @param string $projectId Your Google Cloud project ID.
 * @param string $sourceGcsBucketName The name of the GCS bucket to transfer objects from.
 * @param string $sinkGcsBucketName The name of the GCS bucket to transfer objects to.
 */
function quickstart(
    string $projectId,
    string $sourceGcsBucketName,
    string $sinkGcsBucketName
): void {
    // $project = 'my-project-id';
    // $sourceGcsBucketName = 'my-source-bucket';
    // $sinkGcsBucketName = 'my-sink-bucket';
    $transferJob = new TransferJob([
        'project_id' => $projectId,
        'transfer_spec' => new TransferSpec([
            'gcs_data_sink' => new GcsData(['bucket_name' => $sinkGcsBucketName]),
            'gcs_data_source' => new GcsData(['bucket_name' => $sourceGcsBucketName])
        ]),
        'status' => Status::ENABLED
    ]);

    $client = new StorageTransferServiceClient();
    $createRequest = (new CreateTransferJobRequest())
        ->setTransferJob($transferJob);
    $response = $client->createTransferJob($createRequest);
    $runRequest = (new RunTransferJobRequest())
        ->setJobName($response->getName())
        ->setProjectId($projectId);
    $client->runTransferJob($runRequest);

    printf('Created and ran transfer job from %s to %s with name %s ' . PHP_EOL, $sourceGcsBucketName, $sinkGcsBucketName, $response->getName());
}

Python

如要瞭解如何安裝及使用 Storage 移轉服務用戶端程式庫,請參閱 Storage 移轉服務用戶端程式庫。詳情請參閱 Storage 移轉服務 Python API 參考說明文件

如要向 Storage 移轉服務進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

from google.cloud import storage_transfer


def create_one_time_transfer(
    project_id: str = "my_project_id",
    source_bucket: str = "my_source_bucket",
    sink_bucket: str = "my_sink_bucket",
):
    """Creates a one-time transfer job."""

    client = storage_transfer.StorageTransferServiceClient()

    # The ID of the Google Cloud Platform Project that owns the job
    # project_id = 'my-project-id'

    # Google Cloud Storage source bucket name
    # source_bucket = 'my-gcs-source-bucket'

    # Google Cloud Storage destination bucket name
    # sink_bucket = 'my-gcs-destination-bucket'

    transfer_job_request = storage_transfer.CreateTransferJobRequest(
        {
            "transfer_job": {
                "project_id": project_id,
                "status": storage_transfer.TransferJob.Status.ENABLED,
                "transfer_spec": {
                    "gcs_data_source": {
                        "bucket_name": source_bucket,
                    },
                    "gcs_data_sink": {
                        "bucket_name": sink_bucket,
                    },
                },
            }
        }
    )

    transfer_job = client.create_transfer_job(transfer_job_request)
    client.run_transfer_job({"job_name": transfer_job.name, "project_id": project_id})

    print(f"Created and ran transfer job: {transfer_job.name}")

Ruby

如要瞭解如何安裝及使用 Storage 移轉服務用戶端程式庫,請參閱 Storage 移轉服務用戶端程式庫。詳情請參閱 Storage 移轉服務 Ruby API 參考說明文件

如要向 Storage 移轉服務進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

def quickstart project_id:, gcs_source_bucket:, gcs_sink_bucket:
  # Your Google Cloud Project ID
  # project_id = "your-project_id"

  # The name of the source GCS bucket to transfer objects from
  # gcs_source_bucket = "your-source-gcs-source-bucket"

  # The name of the  GCS bucket to transfer objects to
  # gcs_sink_bucket = "your-sink-gcs-bucket"

  require "google/cloud/storage_transfer"

  transfer_job = {
    project_id: project_id,
    transfer_spec: {
      gcs_data_source: {
        bucket_name: gcs_source_bucket
      },
      gcs_data_sink: {
        bucket_name: gcs_sink_bucket
      }
    },
    status: :ENABLED
  }

  client = Google::Cloud::StorageTransfer.storage_transfer_service

  transfer_job_response = client.create_transfer_job transfer_job: transfer_job

  run_request = {
    project_id: project_id,
    job_name: transfer_job_response.name
  }
  client.run_transfer_job run_request

  puts "Created and ran transfer job between #{gcs_source_bucket} and #{gcs_sink_bucket} with name #{transfer_job_response.name}"
end

搭配 Cloud Shell 編輯器使用用戶端程式庫

Java


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


Node.js


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


Python


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


其他資源