27
27
import com .google .api .services .storagetransfer .model .TransferSpec ;
28
28
29
29
import java .io .IOException ;
30
- import java .util . logging . Logger ;
30
+ import java .io . PrintStream ;
31
31
32
32
/**
33
33
* Creates a one-off transfer job from Amazon S3 to Google Cloud Storage.
34
34
*/
35
35
public final class AwsRequester {
36
-
37
- private static final String JOB_DESC = "YOUR DESCRIPTION" ;
38
- private static final String PROJECT_ID = "YOUR_PROJECT_ID" ;
39
- private static final String AWS_SOURCE_NAME = "YOUR SOURCE BUCKET" ;
40
- private static final String AWS_ACCESS_KEY_ID = "YOUR_ACCESS_KEY_ID" ;
41
- private static final String AWS_SECRET_ACCESS_KEY = "YOUR_SECRET_ACCESS_KEY" ;
42
- private static final String GCS_SINK_NAME = "YOUR_SINK_BUCKET" ;
43
-
44
- /**
45
- * Specify times below using US Pacific Time Zone.
46
- */
47
- private static final String START_DATE = "YYYY-MM-DD" ;
48
- private static final String START_TIME = "HH:MM:SS" ;
49
-
50
- private static final Logger LOG = Logger .getLogger (AwsRequester .class .getName ());
51
-
52
36
/**
53
37
* Creates and executes a request for a TransferJob from Amazon S3 to Cloud Storage.
54
38
*
39
+ * <p>The {@code startDate} and {@code startTime} parameters should be set according to the UTC
40
+ * Time Zone. See:
41
+ * https://developers.google.com/resources/api-libraries/documentation/storagetransfer/v1/java/latest/com/google/api/services/storagetransfer/v1/model/Schedule.html#getStartTimeOfDay()
42
+ *
55
43
* @return the response TransferJob if the request is successful
56
44
* @throws InstantiationException
57
45
* if instantiation fails when building the TransferJob
@@ -60,43 +48,73 @@ public final class AwsRequester {
60
48
* @throws IOException
61
49
* if the client failed to complete the request
62
50
*/
63
- public static TransferJob createAwsTransferJob () throws InstantiationException ,
64
- IllegalAccessException , IOException {
65
- Date date = TransferJobUtils .createDate (START_DATE );
66
- TimeOfDay time = TransferJobUtils .createTimeOfDay (START_TIME );
67
- TransferJob transferJob = TransferJob .class
68
- .newInstance ()
69
- .setDescription (JOB_DESC )
70
- .setProjectId (PROJECT_ID )
71
- .setTransferSpec (
72
- TransferSpec .class
73
- .newInstance ()
74
- .setAwsS3DataSource (
75
- AwsS3Data .class
76
- .newInstance ()
77
- .setBucketName (AWS_SOURCE_NAME )
78
- .setAwsAccessKey (
79
- AwsAccessKey .class .newInstance ().setAccessKeyId (AWS_ACCESS_KEY_ID )
80
- .setSecretAccessKey (AWS_SECRET_ACCESS_KEY )))
81
- .setGcsDataSink (GcsData .class .newInstance ().setBucketName (GCS_SINK_NAME )))
82
- .setSchedule (
83
- Schedule .class .newInstance ().setScheduleStartDate (date ).setScheduleEndDate (date )
84
- .setStartTimeOfDay (time )).setStatus ("ENABLED" );
51
+ public static TransferJob createAwsTransferJob (
52
+ String projectId ,
53
+ String jobDescription ,
54
+ String awsSourceBucket ,
55
+ String gcsSinkBucket ,
56
+ String startDate ,
57
+ String startTime ,
58
+ String awsAccessKeyId ,
59
+ String awsSecretAccessKey )
60
+ throws InstantiationException , IllegalAccessException , IOException {
61
+ Date date = TransferJobUtils .createDate (startDate );
62
+ TimeOfDay time = TransferJobUtils .createTimeOfDay (startTime );
63
+ TransferJob transferJob =
64
+ new TransferJob ()
65
+ .setDescription (jobDescription )
66
+ .setProjectId (projectId )
67
+ .setTransferSpec (
68
+ new TransferSpec ()
69
+ .setAwsS3DataSource (
70
+ new AwsS3Data ()
71
+ .setBucketName (awsSourceBucket )
72
+ .setAwsAccessKey (
73
+ new AwsAccessKey ()
74
+ .setAccessKeyId (awsAccessKeyId )
75
+ .setSecretAccessKey (awsSecretAccessKey )))
76
+ .setGcsDataSink (new GcsData ().setBucketName (gcsSinkBucket )))
77
+ .setSchedule (
78
+ new Schedule ()
79
+ .setScheduleStartDate (date )
80
+ .setScheduleEndDate (date )
81
+ .setStartTimeOfDay (time ))
82
+ .setStatus ("ENABLED" );
85
83
86
84
Storagetransfer client = TransferClientCreator .createStorageTransferClient ();
87
85
return client .transferJobs ().create (transferJob ).execute ();
88
86
}
89
87
88
+ public static void run (PrintStream out )
89
+ throws InstantiationException , IllegalAccessException , IOException {
90
+ String projectId = TransferJobUtils .getPropertyOrFail ("projectId" );
91
+ String jobDescription = TransferJobUtils .getPropertyOrFail ("jobDescription" );
92
+ String awsSourceBucket = TransferJobUtils .getPropertyOrFail ("awsSourceBucket" );
93
+ String gcsSinkBucket = TransferJobUtils .getPropertyOrFail ("gcsSinkBucket" );
94
+ String startDate = TransferJobUtils .getPropertyOrFail ("startDate" );
95
+ String startTime = TransferJobUtils .getPropertyOrFail ("startTime" );
96
+ String awsAccessKeyId = TransferJobUtils .getEnvOrFail ("AWS_ACCESS_KEY_ID" );
97
+ String awsSecretAccessKey = TransferJobUtils .getEnvOrFail ("AWS_SECRET_ACCESS_KEY" );
98
+
99
+ TransferJob responseT =
100
+ createAwsTransferJob (
101
+ projectId ,
102
+ jobDescription ,
103
+ awsSourceBucket ,
104
+ gcsSinkBucket ,
105
+ startDate ,
106
+ startTime ,
107
+ awsAccessKeyId ,
108
+ awsSecretAccessKey );
109
+ out .println ("Return transferJob: " + responseT .toPrettyString ());
110
+ }
111
+
90
112
/**
91
113
* Output the contents of a successfully created TransferJob.
92
- *
93
- * @param args
94
- * arguments from the command line
95
114
*/
96
115
public static void main (String [] args ) {
97
116
try {
98
- TransferJob responseT = createAwsTransferJob ();
99
- LOG .info ("Return transferJob: " + responseT .toPrettyString ());
117
+ run (System .out );
100
118
} catch (Exception e ) {
101
119
e .printStackTrace ();
102
120
}
0 commit comments