gpt4 book ai didi

java - 使用 JAVA 以编程方式将 S3 对象(公共(public) URL)发送至 Google 云存储

转载 作者:行者123 更新时间:2023-12-02 09:09:00 24 4
gpt4 key购买 nike

想要使用 Java 以编程方式将文件从 AWS S3(可用公共(public) URL)传输到 Google 云存储。

每个 S3 文件均由第三方自动生成,并且每天提供 URL。为了完全访问这些数据,我想将这些文件传输到我们的 Google 云存储以供进一步分析。

无法在预定时间创建传输作业,因为 S3 URL 是随机的,我们会在每日检索时获知。

我在java中找不到任何与gsutil相关的代码。

最佳答案

您可以使用 Google 库,如 this guide 。为了开始传输数据,您首先需要创建一个传输作业。传输作业管理和协调您的数据传输。您可以从 Google Cloud Console 创建传输作业,如文档 here 所示。 ,或者您可以通过编程方式执行此操作,如下所示:

package com.google.cloud.storage.storagetransfer.samples;

import com.google.api.services.storagetransfer.v1.Storagetransfer;
import com.google.api.services.storagetransfer.v1.model.AwsAccessKey;
import com.google.api.services.storagetransfer.v1.model.AwsS3Data;
import com.google.api.services.storagetransfer.v1.model.Date;
import com.google.api.services.storagetransfer.v1.model.GcsData;
import com.google.api.services.storagetransfer.v1.model.Schedule;
import com.google.api.services.storagetransfer.v1.model.TimeOfDay;
import com.google.api.services.storagetransfer.v1.model.TransferJob;
import com.google.api.services.storagetransfer.v1.model.TransferSpec;
import java.io.IOException;
import java.io.PrintStream;

/**
* Creates a one-off transfer job from Amazon S3 to Google Cloud Storage.
*/
public final class AwsRequester {
/**
* Creates and executes a request for a TransferJob from Amazon S3 to Cloud Storage.
*
* <p>The {@code startDate} and {@code startTime} parameters should be set according to the UTC
* Time Zone. See:
* https://developers.google.com/resources/api-libraries/documentation/storagetransfer/v1/java/latest/com/google/api/services/storagetransfer/v1/model/Schedule.html#getStartTimeOfDay()
*
* @return the response TransferJob if the request is successful
* @throws InstantiationException
* if instantiation fails when building the TransferJob
* @throws IllegalAccessException
* if an illegal access occurs when building the TransferJob
* @throws IOException
* if the client failed to complete the request
*/
public static TransferJob createAwsTransferJob(
String projectId,
String jobDescription,
String awsSourceBucket,
String gcsSinkBucket,
String startDate,
String startTime,
String awsAccessKeyId,
String awsSecretAccessKey)
throws InstantiationException, IllegalAccessException, IOException {
Date date = TransferJobUtils.createDate(startDate);
TimeOfDay time = TransferJobUtils.createTimeOfDay(startTime);
TransferJob transferJob =
new TransferJob()
.setDescription(jobDescription)
.setProjectId(projectId)
.setTransferSpec(
new TransferSpec()
.setAwsS3DataSource(
new AwsS3Data()
.setBucketName(awsSourceBucket)
.setAwsAccessKey(
new AwsAccessKey()
.setAccessKeyId(awsAccessKeyId)
.setSecretAccessKey(awsSecretAccessKey)))
.setGcsDataSink(new GcsData().setBucketName(gcsSinkBucket)))
.setSchedule(
new Schedule()
.setScheduleStartDate(date)
.setScheduleEndDate(date)
.setStartTimeOfDay(time))
.setStatus("ENABLED");

Storagetransfer client = TransferClientCreator.createStorageTransferClient();
return client.transferJobs().create(transferJob).execute();
}

public static void run(PrintStream out)
throws InstantiationException, IllegalAccessException, IOException {
String projectId = TransferJobUtils.getPropertyOrFail("projectId");
String jobDescription = TransferJobUtils.getPropertyOrFail("jobDescription");
String awsSourceBucket = TransferJobUtils.getPropertyOrFail("awsSourceBucket");
String gcsSinkBucket = TransferJobUtils.getPropertyOrFail("gcsSinkBucket");
String startDate = TransferJobUtils.getPropertyOrFail("startDate");
String startTime = TransferJobUtils.getPropertyOrFail("startTime");
String awsAccessKeyId = TransferJobUtils.getEnvOrFail("AWS_ACCESS_KEY_ID");
String awsSecretAccessKey = TransferJobUtils.getEnvOrFail("AWS_SECRET_ACCESS_KEY");

TransferJob responseT =
createAwsTransferJob(
projectId,
jobDescription,
awsSourceBucket,
gcsSinkBucket,
startDate,
startTime,
awsAccessKeyId,
awsSecretAccessKey);
out.println("Return transferJob: " + responseT.toPrettyString());
}

/**
* Output the contents of a successfully created TransferJob.
*/
public static void main(String[] args) {
try {
run(System.out);
} catch (Exception e) {
e.printStackTrace();
}
}
}

关于java - 使用 JAVA 以编程方式将 S3 对象(公共(public) URL)发送至 Google 云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59559829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com