|
| 1 | +/* |
| 2 | + * Copyright 2021 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage.storagetransfer.samples; |
| 18 | + |
| 19 | +// [START storagetransfer_transfer_from_aws] |
| 20 | + |
| 21 | +import com.google.storagetransfer.v1.proto.StorageTransferServiceClient; |
| 22 | +import com.google.storagetransfer.v1.proto.TransferProto.CreateTransferJobRequest; |
| 23 | +import com.google.storagetransfer.v1.proto.TransferTypes.AwsAccessKey; |
| 24 | +import com.google.storagetransfer.v1.proto.TransferTypes.AwsS3Data; |
| 25 | +import com.google.storagetransfer.v1.proto.TransferTypes.GcsData; |
| 26 | +import com.google.storagetransfer.v1.proto.TransferTypes.Schedule; |
| 27 | +import com.google.storagetransfer.v1.proto.TransferTypes.TransferJob; |
| 28 | +import com.google.storagetransfer.v1.proto.TransferTypes.TransferJob.Status; |
| 29 | +import com.google.storagetransfer.v1.proto.TransferTypes.TransferSpec; |
| 30 | +import com.google.type.Date; |
| 31 | +import com.google.type.TimeOfDay; |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.Calendar; |
| 34 | + |
| 35 | +public class TransferFromAws { |
| 36 | + |
| 37 | + // Creates a one-off transfer job from Amazon S3 to Google Cloud Storage. |
| 38 | + public static void transferFromAws( |
| 39 | + String projectId, |
| 40 | + String jobDescription, |
| 41 | + String awsSourceBucket, |
| 42 | + String gcsSinkBucket, |
| 43 | + long startDateTime, |
| 44 | + String awsAccessKeyId, |
| 45 | + String awsSecretAccessKey) |
| 46 | + throws IOException { |
| 47 | + |
| 48 | + // Your Google Cloud Project ID |
| 49 | + // String projectId = "your-project-id"; |
| 50 | + |
| 51 | + // A short description of this job |
| 52 | + // String jobDescription = "Sample transfer job from S3 to GCS."; |
| 53 | + |
| 54 | + // The name of the source AWS bucket to transfer data from |
| 55 | + // String awsSourceBucket = "yourAwsSourceBucket"; |
| 56 | + |
| 57 | + // The name of the GCS bucket to transfer data to |
| 58 | + // String gcsSinkBucket = "your-gcs-bucket"; |
| 59 | + |
| 60 | + // What day and time in UTC to start the transfer, expressed as an epoch date timestamp. |
| 61 | + // If this is in the past relative to when the job is created, it will run the next day. |
| 62 | + // long startDateTime = |
| 63 | + // new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2000-01-01 00:00:00").getTime(); |
| 64 | + |
| 65 | + // The ID used to access your AWS account. Should be accessed via environment variable. |
| 66 | + // String awsAccessKeyId = System.getenv("AWS_ACCESS_KEY_ID"); |
| 67 | + |
| 68 | + // The Secret Key used to access your AWS account. Should be accessed via environment variable. |
| 69 | + // String awsSecretAccessKey = System.getenv("AWS_SECRET_ACCESS_KEY"); |
| 70 | + |
| 71 | + // Set up source and sink |
| 72 | + TransferSpec transferSpec = |
| 73 | + TransferSpec.newBuilder() |
| 74 | + .setAwsS3DataSource( |
| 75 | + AwsS3Data.newBuilder() |
| 76 | + .setBucketName(awsSourceBucket) |
| 77 | + .setAwsAccessKey( |
| 78 | + AwsAccessKey.newBuilder() |
| 79 | + .setAccessKeyId(awsAccessKeyId) |
| 80 | + .setSecretAccessKey(awsSecretAccessKey))) |
| 81 | + .setGcsDataSink(GcsData.newBuilder().setBucketName(gcsSinkBucket)) |
| 82 | + .build(); |
| 83 | + |
| 84 | + // Parse epoch timestamp into the model classes |
| 85 | + Calendar startCalendar = Calendar.getInstance(); |
| 86 | + startCalendar.setTimeInMillis(startDateTime); |
| 87 | + // Note that this is a Date from the model class package, not a java.util.Date |
| 88 | + Date startDate = |
| 89 | + Date.newBuilder() |
| 90 | + .setYear(startCalendar.get(Calendar.YEAR)) |
| 91 | + .setMonth(startCalendar.get(Calendar.MONTH) + 1) |
| 92 | + .setDay(startCalendar.get(Calendar.DAY_OF_MONTH)) |
| 93 | + .build(); |
| 94 | + TimeOfDay startTime = |
| 95 | + TimeOfDay.newBuilder() |
| 96 | + .setHours(startCalendar.get(Calendar.HOUR_OF_DAY)) |
| 97 | + .setMinutes(startCalendar.get(Calendar.MINUTE)) |
| 98 | + .setSeconds(startCalendar.get(Calendar.SECOND)) |
| 99 | + .build(); |
| 100 | + Schedule schedule = |
| 101 | + Schedule.newBuilder() |
| 102 | + .setScheduleStartDate(startDate) |
| 103 | + .setScheduleEndDate(startDate) |
| 104 | + .setStartTimeOfDay(startTime) |
| 105 | + .build(); |
| 106 | + |
| 107 | + // Set up the transfer job |
| 108 | + TransferJob transferJob = |
| 109 | + TransferJob.newBuilder() |
| 110 | + .setDescription(jobDescription) |
| 111 | + .setProjectId(projectId) |
| 112 | + .setTransferSpec(transferSpec) |
| 113 | + .setSchedule(schedule) |
| 114 | + .setStatus(Status.ENABLED) |
| 115 | + .build(); |
| 116 | + |
| 117 | + // Create a Transfer Service client |
| 118 | + StorageTransferServiceClient storageTransfer = StorageTransferServiceClient.create(); |
| 119 | + |
| 120 | + // Create the transfer job |
| 121 | + TransferJob response = |
| 122 | + storageTransfer.createTransferJob( |
| 123 | + CreateTransferJobRequest.newBuilder().setTransferJob(transferJob).build()); |
| 124 | + |
| 125 | + System.out.println("Created transfer job from AWS to GCS:"); |
| 126 | + System.out.println(response.toString()); |
| 127 | + } |
| 128 | +} |
| 129 | +// [END storagetransfer_transfer_from_aws] |
0 commit comments