-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add storagetransfer samples #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Transfer Service sample using Java | ||
|
|
||
| This app creates two types of transfers using the Transfer Service tool. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| 1. Set up a project on Google Developers Console. | ||
| 1. Go to the [Developers Console](https://cloud.google.com/console) and create or select your project. | ||
| You will need the project ID later. | ||
| 1. Within Developers Console, select APIs & auth > Credentials. | ||
| 1. Select Add credentials > Service account > JSON key. | ||
| 1. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to your JSON key. | ||
| 1. Add the Storage Transfer service account, cloud-mobility@system.gserviceaccount.com as an | ||
| editor of your project. | ||
| 1. Set up gcloud for application default credentials. | ||
| 1. `gcloud components update` | ||
| 1. `gcloud auth login` | ||
| 1. `gcloud config set project PROJECT_ID` | ||
|
|
||
| ## Transfer from Amazon S3 to Google Cloud Storage | ||
|
|
||
| Creating a one-time transfer from Amazon S3 to Google Cloud Storage. | ||
| 1. Set up data sink. | ||
| 1. Go to the Developers Console and create a bucket under Cloud Storage > Storage Browser. | ||
| 1. Set up data source. | ||
| 1. Go to AWS Management Console and create a bucket. | ||
| 1. Under Security Credentials, create an IAM User with access to the bucket. | ||
| 1. Create an Access Key for the user. Note the Access Key ID and Secret Access Key. | ||
| 1. In AwsRequester.java, fill in the user-provided constants. | ||
| 1. Run with `mvn compile` and | ||
| `mvn exec:java -Dexec.mainClass="com.google.cloud.storage.storagetransfer.samples.AwsRequester"` | ||
| 1. Note the job ID in the returned Transfer Job. | ||
|
|
||
| ## Transfer data from a standard Cloud Storage bucket to a Cloud Storage Nearline bucket | ||
|
|
||
| Creating a daily transfer from a standard Cloud Storage bucket to a Cloud Storage Nearline | ||
| bucket for files untouched for 30 days. | ||
| 1. Set up data sink. | ||
| 1. Go to the Developers Console and create a bucket under Cloud Storage > Storage Browser. | ||
| 1. Select Nearline for Storage Class. | ||
| 1. Set up data source. | ||
| 1. Go to the Developers Console and create a bucket under Cloud Storage > Storage Browser. | ||
| 1. In NearlineRequester.java, fill in the user-provided constants. | ||
| 1. Run with `mvn compile` and | ||
| `mvn exec:java -Dexec.mainClass="com.google.cloud.storage.storagetransfer.samples.NearlineRequester"` | ||
| 1. Note the job ID in the returned Transfer Job. | ||
|
|
||
| ## Checking the status of a transfer | ||
|
|
||
| 1. In RequestChecker.java, fill in the user-provided constants. Use the Job Name you recorded earlier. | ||
| 1. Run with `mvn compile` and | ||
| `mvn exec:java -Dexec.mainClass="com.google.cloud.storage.storagetransfer.samples.RequestChecker"` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| ~ Copyright (c) 2015 Google Inc. All Rights Reserved. | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); you | ||
| ~ may not use this file except in compliance with the License. You may | ||
| ~ obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| ~ implied. See the License for the specific language governing | ||
| ~ permissions and limitations under the License. | ||
| --> | ||
|
|
||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <artifactId>doc-samples</artifactId> | ||
| <groupId>com.google.cloud</groupId> | ||
| <version>1.0.0</version> | ||
| <relativePath>../..</relativePath> | ||
| </parent> | ||
|
|
||
| <groupId>com.google.storagetransfer.samples</groupId> | ||
| <artifactId>transfersample</artifactId> | ||
| <version>0.1</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>transfersample</name> | ||
| <url>http://maven.apache.org</url> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| <powermock.version>1.6.2</powermock.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.apis</groupId> | ||
| <artifactId>google-api-services-storagetransfer</artifactId> | ||
| <version>v1-rev1-1.20.0</version> | ||
| </dependency> | ||
|
|
||
| <!-- Test Dependencies --> | ||
| <dependency> | ||
| <groupId>org.powermock</groupId> | ||
| <artifactId>powermock-module-junit4</artifactId> | ||
| <version>${powermock.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.powermock</groupId> | ||
| <artifactId>powermock-api-mockito</artifactId> | ||
| <version>${powermock.version}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <version>3.1</version> | ||
| <configuration> | ||
| <source>1.7</source> | ||
| <target>1.7</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> |
104 changes: 104 additions & 0 deletions
104
...transfer/src/main/java/com/google/cloud/storage/storagetransfer/samples/AwsRequester.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /** | ||
| * Copyright 2015 Google Inc. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.storage.storagetransfer.samples; | ||
|
|
||
| import com.google.api.services.storagetransfer.Storagetransfer; | ||
| import com.google.api.services.storagetransfer.model.AwsAccessKey; | ||
| import com.google.api.services.storagetransfer.model.AwsS3Data; | ||
| import com.google.api.services.storagetransfer.model.Date; | ||
| import com.google.api.services.storagetransfer.model.GcsData; | ||
| import com.google.api.services.storagetransfer.model.Schedule; | ||
| import com.google.api.services.storagetransfer.model.TimeOfDay; | ||
| import com.google.api.services.storagetransfer.model.TransferJob; | ||
| import com.google.api.services.storagetransfer.model.TransferSpec; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.logging.Logger; | ||
|
|
||
| /** | ||
| * Creates a one-off transfer job from Amazon S3 to Google Cloud Storage. | ||
| */ | ||
| public final class AwsRequester { | ||
|
|
||
| private static final String JOB_DESC = "YOUR DESCRIPTION"; | ||
| private static final String PROJECT_ID = "YOUR_PROJECT_ID"; | ||
| private static final String AWS_SOURCE_NAME = "YOUR SOURCE BUCKET"; | ||
| private static final String AWS_ACCESS_KEY_ID = "YOUR_ACCESS_KEY_ID"; | ||
| private static final String AWS_SECRET_ACCESS_KEY = "YOUR_SECRET_ACCESS_KEY"; | ||
| private static final String GCS_SINK_NAME = "YOUR_SINK_BUCKET"; | ||
|
|
||
| /** | ||
| * Specify times below using US Pacific Time Zone. | ||
| */ | ||
| private static final String START_DATE = "YYYY-MM-DD"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might wish to have a line break as folks aren't going to be setting this? Or if they are, why? |
||
| private static final String START_TIME = "HH:MM:SS"; | ||
|
|
||
| private static final Logger LOG = Logger.getLogger(AwsRequester.class.getName()); | ||
|
|
||
| /** | ||
| * Creates and executes a request for a TransferJob from Amazon S3 to Cloud Storage. | ||
| * | ||
| * @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() throws InstantiationException, | ||
| IllegalAccessException, IOException { | ||
| Date date = TransferJobUtils.createDate(START_DATE); | ||
| TimeOfDay time = TransferJobUtils.createTimeOfDay(START_TIME); | ||
| TransferJob transferJob = TransferJob.class | ||
| .newInstance() | ||
| .setDescription(JOB_DESC) | ||
| .setProjectId(PROJECT_ID) | ||
| .setTransferSpec( | ||
| TransferSpec.class | ||
| .newInstance() | ||
| .setAwsS3DataSource( | ||
| AwsS3Data.class | ||
| .newInstance() | ||
| .setBucketName(AWS_SOURCE_NAME) | ||
| .setAwsAccessKey( | ||
| AwsAccessKey.class.newInstance().setAccessKeyId(AWS_ACCESS_KEY_ID) | ||
| .setSecretAccessKey(AWS_SECRET_ACCESS_KEY))) | ||
| .setGcsDataSink(GcsData.class.newInstance().setBucketName(GCS_SINK_NAME))) | ||
| .setSchedule( | ||
| Schedule.class.newInstance().setScheduleStartDate(date).setScheduleEndDate(date) | ||
| .setStartTimeOfDay(time)).setStatus("ENABLED"); | ||
|
|
||
| Storagetransfer client = TransferClientCreator.createStorageTransferClient(); | ||
| return client.transferJobs().create(transferJob).execute(); | ||
| } | ||
|
|
||
| /** | ||
| * Output the contents of a successfully created TransferJob. | ||
| * | ||
| * @param args | ||
| * arguments from the command line | ||
| */ | ||
| public static void main(String[] args) { | ||
| try { | ||
| TransferJob responseT = createAwsTransferJob(); | ||
| LOG.info("Return transferJob: " + responseT.toPrettyString()); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
101 changes: 101 additions & 0 deletions
101
...fer/src/main/java/com/google/cloud/storage/storagetransfer/samples/NearlineRequester.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /** | ||
| * Copyright 2015 Google Inc. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.storage.storagetransfer.samples; | ||
|
|
||
| import com.google.api.services.storagetransfer.Storagetransfer; | ||
| import com.google.api.services.storagetransfer.model.Date; | ||
| import com.google.api.services.storagetransfer.model.GcsData; | ||
| import com.google.api.services.storagetransfer.model.ObjectConditions; | ||
| import com.google.api.services.storagetransfer.model.Schedule; | ||
| import com.google.api.services.storagetransfer.model.TimeOfDay; | ||
| import com.google.api.services.storagetransfer.model.TransferJob; | ||
| import com.google.api.services.storagetransfer.model.TransferOptions; | ||
| import com.google.api.services.storagetransfer.model.TransferSpec; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.logging.Logger; | ||
|
|
||
| /** | ||
| * Creates a daily transfer from a standard Cloud Storage bucket to a Cloud Storage Nearline | ||
| * bucket for files untouched for 30 days. | ||
| */ | ||
| public final class NearlineRequester { | ||
|
|
||
| private static final String JOB_DESC = "YOUR DESCRIPTION"; | ||
| private static final String PROJECT_ID = "YOUR_PROJECT_ID"; | ||
| private static final String GCS_SOURCE_NAME = "YOUR_SOURCE_BUCKET"; | ||
| private static final String NEARLINE_SINK_NAME = "YOUR_SINK_BUCKET"; | ||
|
|
||
| /** | ||
| * Specify times below using US Pacific Time Zone. | ||
| */ | ||
| private static final String START_DATE = "YYYY-MM-DD"; | ||
| private static final String START_TIME = "HH:MM:SS"; | ||
|
|
||
| private static final Logger LOG = Logger.getLogger(AwsRequester.class.getName()); | ||
|
|
||
| /** | ||
| * Creates and executes a request for a TransferJob to Cloud Storage Nearline. | ||
| * | ||
| * @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 createNearlineTransferJob() throws InstantiationException, | ||
| IllegalAccessException, IOException { | ||
| Date date = TransferJobUtils.createDate(START_DATE); | ||
| TimeOfDay time = TransferJobUtils.createTimeOfDay(START_TIME); | ||
| TransferJob transferJob = TransferJob.class | ||
| .newInstance() | ||
| .setDescription(JOB_DESC) | ||
| .setProjectId(PROJECT_ID) | ||
| .setTransferSpec( | ||
| TransferSpec.class | ||
| .newInstance() | ||
| .setGcsDataSource(GcsData.class.newInstance().setBucketName(GCS_SOURCE_NAME)) | ||
| .setGcsDataSink(GcsData.class.newInstance().setBucketName(NEARLINE_SINK_NAME)) | ||
| .setObjectConditions( | ||
| ObjectConditions.class.newInstance().setMinTimeElapsedSinceLastModification("2592000s")) | ||
| .setTransferOptions( | ||
| TransferOptions.class.newInstance().setDeleteObjectsFromSourceAfterTransfer(true))) | ||
| .setSchedule(Schedule.class.newInstance().setScheduleStartDate(date) | ||
| .setStartTimeOfDay(time)) | ||
| .setStatus("ENABLED"); | ||
|
|
||
| Storagetransfer client = TransferClientCreator.createStorageTransferClient(); | ||
| return client.transferJobs().create(transferJob).execute(); | ||
| } | ||
|
|
||
| /** | ||
| * Output the contents of a successfully created TransferJob. | ||
| * | ||
| * @param args | ||
| * arguments from the command line | ||
| */ | ||
| public static void main(String[] args) { | ||
| try { | ||
| TransferJob responseT = createNearlineTransferJob(); | ||
| LOG.info("Return transferJob: " + responseT.toPrettyString()); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did I get this jar?