Skip to content

Commit 89abd10

Browse files
feat: Increase STS Sample Coverage (#42)
* test: Prepare for multiple samples and tests * feat: Increase STS Sample Coverage * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * style: lint * ci: Add `pre-samples-test.sh` for AWS integration testing * style: Streamline error handling Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 94fe9de commit 89abd10

18 files changed

+1170
-125
lines changed

storagetransfer/aws-request.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright 2022 Google LLC
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+
'use strict';
18+
19+
async function main(
20+
projectId,
21+
description,
22+
awsSourceBucket,
23+
awsAccessKeyId,
24+
awsSecretAccessKey,
25+
gcsSinkBucket
26+
) {
27+
// [START storagetransfer_transfer_from_aws]
28+
29+
// Imports the Google Cloud client library
30+
const {
31+
StorageTransferServiceClient,
32+
} = require('@google-cloud/storage-transfer');
33+
34+
/**
35+
* TODO(developer): Uncomment the following lines before running the sample.
36+
*/
37+
// The ID of the Google Cloud Platform Project that owns the job
38+
// projectId = 'my-project-id'
39+
40+
// A useful description for your transfer job
41+
// description = 'My transfer job'
42+
43+
// AWS S3 source bucket name
44+
// awsSourceBucket = 'my-s3-source-bucket'
45+
46+
// AWS Access Key ID
47+
// awsAccessKeyId = 'AKIA...'
48+
49+
// AWS Secret Access Key
50+
// awsSecretAccessKey = 'HEAoMK2.../...ku8'
51+
52+
// Google Cloud Storage destination bucket name
53+
// gcsSinkBucket = 'my-gcs-destination-bucket'
54+
55+
// Creates a client
56+
const client = new StorageTransferServiceClient();
57+
58+
/**
59+
* Creates a one-time transfer job from Amazon S3 to Google Cloud Storage.
60+
*/
61+
async function transferFromS3() {
62+
// Setting the start date and the end date as the same time creates a
63+
// one-time transfer
64+
const now = new Date();
65+
const oneTimeSchedule = {
66+
day: now.getDate(),
67+
month: now.getMonth() + 1,
68+
year: now.getFullYear(),
69+
};
70+
71+
// Runs the request and creates the job
72+
const [transferJob] = await client.createTransferJob({
73+
transferJob: {
74+
projectId,
75+
description,
76+
status: 'ENABLED',
77+
schedule: {
78+
scheduleStartDate: oneTimeSchedule,
79+
scheduleEndDate: oneTimeSchedule,
80+
},
81+
transferSpec: {
82+
awsS3DataSource: {
83+
bucketName: awsSourceBucket,
84+
awsAccessKey: {
85+
accessKeyId: awsAccessKeyId,
86+
secretAccessKey: awsSecretAccessKey,
87+
},
88+
},
89+
gcsDataSink: {
90+
bucketName: gcsSinkBucket,
91+
},
92+
},
93+
},
94+
});
95+
96+
console.log(
97+
`Created and ran a transfer job from '${awsSourceBucket}' to '${gcsSinkBucket}' with name ${transferJob.name}`
98+
);
99+
}
100+
101+
transferFromS3();
102+
// [END storagetransfer_transfer_from_aws]
103+
}
104+
105+
main(...process.argv.slice(2));
106+
107+
process.on('unhandledRejection', err => {
108+
console.error(err);
109+
process.exitCode = 1;
110+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2022 Google LLC
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+
'use strict';
18+
19+
async function main(projectId, jobName) {
20+
// [START storagetransfer_get_latest_transfer_operation]
21+
22+
// Imports the Google Cloud client library
23+
const {
24+
StorageTransferServiceClient,
25+
} = require('@google-cloud/storage-transfer');
26+
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of the Google Cloud Platform Project that owns the job
31+
// projectId = 'my-project-id'
32+
33+
// Storage Transfer Service job name
34+
// jobName = 'transferJobs/1234567890'
35+
36+
// Creates a client
37+
const client = new StorageTransferServiceClient();
38+
39+
/**
40+
* Checks the latest transfer operation for a given transfer job.
41+
*/
42+
async function checkLatestTransferOperation() {
43+
const [transferJob] = await client.getTransferJob({projectId, jobName});
44+
45+
if (transferJob.latestOperationName) {
46+
const [transferOperation] = await client.operationsClient.getOperation({
47+
name: transferJob.latestOperationName,
48+
});
49+
50+
const operation = JSON.stringify(transferOperation, null, 2);
51+
52+
console.log(`Latest transfer operation for '${jobName}': ${operation}`);
53+
} else {
54+
console.log(`Transfer job '${jobName}' has not ran yet.`);
55+
}
56+
}
57+
58+
checkLatestTransferOperation();
59+
// [END storagetransfer_get_latest_transfer_operation]
60+
}
61+
62+
main(...process.argv.slice(2));
63+
64+
process.on('unhandledRejection', err => {
65+
console.error(err);
66+
process.exitCode = 1;
67+
});
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Copyright 2022 Google LLC
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+
'use strict';
18+
19+
async function main(projectId, jobName, maxRetryDelayMillis) {
20+
// [START storagetransfer_create_retry_handler]
21+
22+
// Imports the Google Cloud client library
23+
const {
24+
StorageTransferServiceClient,
25+
} = require('@google-cloud/storage-transfer');
26+
27+
/**
28+
* TODO(developer): Uncomment the following lines before running the sample.
29+
*/
30+
// The ID of the Google Cloud Platform Project that owns the job
31+
// projectId = 'my-project-id'
32+
33+
// Storage Transfer Service job name
34+
// jobName = 'transferJobs/1234567890'
35+
36+
// The maximum delay time, in milliseconds, between requests
37+
// maxRetryDelayMillis = 60000
38+
39+
// Creates a client
40+
const client = new StorageTransferServiceClient();
41+
42+
/**
43+
* Check the latest transfer operation associated with a transfer job
44+
* with retries.
45+
*/
46+
async function getTransferJobWithRetries() {
47+
// Setting the start date and the end date as the same time creates a
48+
// one-time transfer
49+
50+
const options = {
51+
retry: {
52+
backoffSettings: {
53+
maxRetryDelayMillis,
54+
},
55+
},
56+
};
57+
58+
const [transferJob] = await client.getTransferJob(
59+
{projectId, jobName},
60+
options
61+
);
62+
63+
console.log(
64+
`Fetched transfer job: ${transferJob.name} with a maximum of ${maxRetryDelayMillis}ms delay time between requests`
65+
);
66+
}
67+
68+
getTransferJobWithRetries();
69+
// [END storagetransfer_create_retry_handler]
70+
}
71+
72+
const [projectId, jobName, maxRetryDelayMillis] = [...process.argv.slice(2)];
73+
74+
main(projectId, jobName, Number.parseInt(maxRetryDelayMillis));
75+
76+
process.on('unhandledRejection', err => {
77+
console.error(err);
78+
process.exitCode = 1;
79+
});
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* Copyright 2022 Google LLC
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+
'use strict';
18+
19+
async function main(
20+
projectId,
21+
description,
22+
gcsSourceBucket,
23+
gcsSinkBucket,
24+
startDate = new Date()
25+
) {
26+
// [START storagetransfer_transfer_to_nearline]
27+
28+
// Imports the Google Cloud client library
29+
const {
30+
StorageTransferServiceClient,
31+
} = require('@google-cloud/storage-transfer');
32+
33+
/**
34+
* TODO(developer): Uncomment the following lines before running the sample.
35+
*/
36+
// The ID of the Google Cloud Platform Project that owns the job
37+
// projectId = 'my-project-id'
38+
39+
// A useful description for your transfer job
40+
// description = 'My transfer job'
41+
42+
// Google Cloud Storage source bucket name
43+
// gcsSourceBucket = 'my-gcs-source-bucket'
44+
45+
// Google Cloud Storage destination bucket name
46+
// gcsSinkBucket = 'my-gcs-destination-bucket'
47+
48+
// Date to start daily migration
49+
// startDate = new Date()
50+
51+
// Creates a client
52+
const client = new StorageTransferServiceClient();
53+
54+
/**
55+
* Create a daily migration from a GCS bucket to another GCS bucket for
56+
* objects untouched for 30+ days.
57+
*/
58+
async function createDailyNearline30DayMigration() {
59+
// Runs the request and creates the job
60+
const [transferJob] = await client.createTransferJob({
61+
transferJob: {
62+
projectId,
63+
description,
64+
status: 'ENABLED',
65+
schedule: {
66+
scheduleStartDate: {
67+
day: startDate.getDate(),
68+
month: startDate.getMonth() + 1,
69+
year: startDate.getFullYear(),
70+
},
71+
},
72+
transferSpec: {
73+
gcsDataSource: {
74+
bucketName: gcsSourceBucket,
75+
},
76+
gcsDataSink: {
77+
bucketName: gcsSinkBucket,
78+
},
79+
objectConditions: {
80+
minTimeElapsedSinceLastModification: {
81+
seconds: 2592000, // 30 days
82+
},
83+
},
84+
transferOptions: {
85+
deleteObjectsFromSourceAfterTransfer: true,
86+
},
87+
},
88+
},
89+
});
90+
91+
console.log(`Created transferJob: ${transferJob.name}`);
92+
}
93+
94+
createDailyNearline30DayMigration();
95+
// [END storagetransfer_transfer_to_nearline]
96+
}
97+
98+
const [projectId, description, gcsSourceBucket, gcsSinkBucket, startDate] = [
99+
...process.argv.slice(2),
100+
];
101+
102+
main(
103+
projectId,
104+
description,
105+
gcsSourceBucket,
106+
gcsSinkBucket,
107+
new Date(startDate)
108+
);
109+
110+
process.on('unhandledRejection', err => {
111+
console.error(err);
112+
process.exitCode = 1;
113+
});

storagetransfer/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
"*.js"
1111
],
1212
"scripts": {
13-
"test": "c8 mocha --timeout 600000 test/*.js"
13+
"test": "c8 mocha ---parallel --timeout 600000 test/*.test.js"
1414
},
1515
"dependencies": {
1616
"@google-cloud/storage-transfer": "^1.1.1"
1717
},
1818
"devDependencies": {
19-
"chai": "^4.2.0",
19+
"@google-cloud/storage": "^5.15.3",
20+
"aws-sdk": "^2.1073.0",
2021
"c8": "^7.1.0",
22+
"chai": "^4.2.0",
2123
"mocha": "^9.0.0",
22-
"uuid": "^8.0.0",
23-
"@google-cloud/storage": "^5.15.3"
24+
"uuid": "^8.0.0"
2425
}
2526
}

0 commit comments

Comments
 (0)