使用Java在两个Google Cloud Storage存储桶之间复制

huangapple go评论110阅读模式
英文:

Copy between two Google Cloud Storage buckets using Java

问题

复制两个Google Cloud Storage存储桶之间的内容,需要Java SDK中以下命令的等效操作:

gsutil cp gs://bucket-name/filename gs://bucket-name

我有两个选项可以选择其中之一。专家们,您能否建议是否有更好的方法?

  1. 使用Java.lang.Runtime.exec()
  2. 使用Java.lang.ProcessBuilder
英文:

To Copy between two Google Cloud Storage buckets, need equivalent of the below command from Java SDK

gsutil cp gs://bucket-name/filename gs://bucket-name

I have 2 options to use one of the below. Experts can you suggest If there is any better approach ?

  1. Java.lang.Runtime.exec()

  2. Java.lang.ProcessBuilder

答案1

得分: 2

文档中所述,您可以使用Java客户端库将对象从一个存储桶复制到另一个存储桶,作为以下命令的直接替代:

gsutil cp gs://SOURCE_BUCKET_NAME/SOURCE_OBJECT_NAME gs://DESTINATION_BUCKET_NAME/NAME_OF_COPY

以下是示例代码,您可以查看并从github下载完整示例项目,有关所需依赖项,请查看readme文件

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class CopyObject {
  public static void copyObject(
      String projectId, String sourceBucketName, String objectName, String targetBucketName) {
    // GCP项目的ID
    // String projectId = "your-project-id";

    // 原对象所在的存储桶的ID
    // String sourceBucketName = "your-source-bucket";

    // 要复制的GCS对象的ID
    // String objectName = "your-object-name";

    // 要复制对象到的存储桶的ID
    // String targetBucketName = "target-object-bucket";

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Blob blob = storage.get(sourceBucketName, objectName);

    // 这会保持原始名称,您也可以使用copyTo(targetBucketName, "target-object-name")来更改名称
    blob.copyTo(targetBucketName);

    System.out.println(
        "已从存储桶 " + sourceBucketName + " 复制对象 " + objectName + " 到存储桶 " + targetBucketName);
  }
}

请注意,此处提供的是您请求的翻译内容。如果您需要进一步的帮助或有其他问题,请随时提问。

英文:

As is mentioned in the documentation you can use the Java Client library to copy objects between buckets as direct alternative of the command

gsutil cp gs://SOURCE_BUCKET_NAME/SOURCE_OBJECT_NAME gs://DESTINATION_BUCKET_NAME/NAME_OF_COPY

This is the example code, and you can check and download the full example project from github, please check the readme file to know which dependecies are necesaries

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class CopyObject {
  public static void copyObject(
      String projectId, String sourceBucketName, String objectName, String targetBucketName) {
    // The ID of your GCP project
    // String projectId = "your-project-id";

    // The ID of the bucket the original object is in
    // String sourceBucketName = "your-source-bucket";

    // The ID of the GCS object to copy
    // String objectName = "your-object-name";

    // The ID of the bucket to copy the object to
    // String targetBucketName = "target-object-bucket"

    Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
    Blob blob = storage.get(sourceBucketName, objectName);

    // This keeps the original name, you could also do
    // copyTo(targetBucketName, "target-object-name") to change the name
    blob.copyTo(targetBucketName);

    System.out.println(
        "Copied object "
            + objectName
            + " from bucket "
            + sourceBucketName
            + " to "
            + targetBucketName);
  }
}

答案2

得分: 0

如果:1)您希望存储桶内的Blob(文件)名称保持不变,并且2)您有多个(>100)文件需要复制,最佳的方法是使用传输作业。

此作业由Google使用其自己的设备制作,与逐个从一个存储桶复制文件到另一个存储桶相比,速度要快得多,因为它们可以与底层存储进行通信。

请查看Google提供的传输作业Java示例代码API文档

英文:

If 1) you want the blob (file) names inside the buckets to remain the same
and 2) You have several (>100) files to copy the best way to do this is to use a transfer job.

This job is made by google with their own equipment, and it's much faster that copying files one by one from one bucket to another because they can talk with the underlying storage.

Take a lookt at the Google-provided java sample code for transfer job and API Documentation.

huangapple
  • 本文由 发表于 2020年9月22日 19:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64008981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定