Skip to content
This repository has been archived by the owner on Jul 20, 2023. It is now read-only.

feat: Increase STS Sample Coverage #42

Merged
merged 8 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .kokoro/pre-samples-test.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ npm install @google-cloud/storage-transfer

```javascript

// Imports the Google Cloud client library
const {
StorageTransferServiceClient,
} = require('@google-cloud/storage-transfer');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
Expand All @@ -70,14 +75,12 @@ npm install @google-cloud/storage-transfer
// The ID of the GCS bucket to transfer data to
// const gcsSinkBucket = 'my-sink-bucket'

// Imports the Google Cloud client library
const {
StorageTransferServiceClient,
} = require('@google-cloud/storage-transfer');

// Creates a client
const client = new StorageTransferServiceClient();

/**
* Creates a one-time transfer job.
*/
async function quickstart() {
// Creates a request to transfer from the source bucket to
// the sink bucket
Expand All @@ -93,18 +96,16 @@ async function quickstart() {
};

// Runs the request and creates the job
const response = await client.createTransferJob(createRequest);

const jobName = response[0].name;
const [transferJob] = await client.createTransferJob(createRequest);

const runRequest = {
jobName: jobName,
jobName: transferJob.name,
projectId: projectId,
};
await client.runTransferJob(runRequest);

console.log(
`Created and ran a transfer job from ${gcsSourceBucket} to ${gcsSinkBucket} with name ${jobName}`
`Created and ran a transfer job from ${gcsSourceBucket} to ${gcsSinkBucket} with name ${transferJob.name}`
);
}

Expand All @@ -120,7 +121,12 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage-tra

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Aws-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/aws-request.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/aws-request.js,samples/README.md) |
| Check-latest-transfer-operation | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/check-latest-transfer-operation.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/check-latest-transfer-operation.js,samples/README.md) |
| Get-transfer-job-with-retries | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/get-transfer-job-with-retries.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/get-transfer-job-with-retries.js,samples/README.md) |
| Nearline-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/nearline-request.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/nearline-request.js,samples/README.md) |
| Quickstart | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) |
| Transfer-check | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/transfer-check.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/transfer-check.js,samples/README.md) |



Expand Down
90 changes: 90 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

* [Before you begin](#before-you-begin)
* [Samples](#samples)
* [Aws-request](#aws-request)
* [Check-latest-transfer-operation](#check-latest-transfer-operation)
* [Get-transfer-job-with-retries](#get-transfer-job-with-retries)
* [Nearline-request](#nearline-request)
* [Quickstart](#quickstart)
* [Transfer-check](#transfer-check)

## Before you begin

Expand All @@ -29,6 +34,74 @@ Before running the samples, make sure you've followed the steps outlined in



### Aws-request

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/aws-request.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/aws-request.js,samples/README.md)

__Usage:__


`node samples/aws-request.js`


-----




### Check-latest-transfer-operation

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/check-latest-transfer-operation.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/check-latest-transfer-operation.js,samples/README.md)

__Usage:__


`node samples/check-latest-transfer-operation.js`


-----




### Get-transfer-job-with-retries

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/get-transfer-job-with-retries.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/get-transfer-job-with-retries.js,samples/README.md)

__Usage:__


`node samples/get-transfer-job-with-retries.js`


-----




### Nearline-request

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/nearline-request.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/nearline-request.js,samples/README.md)

__Usage:__


`node samples/nearline-request.js`


-----




### Quickstart

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/quickstart.js).
Expand All @@ -41,6 +114,23 @@ __Usage:__
`node samples/quickstart.js`


-----




### Transfer-check

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/transfer-check.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage-transfer&page=editor&open_in_editor=samples/transfer-check.js,samples/README.md)

__Usage:__


`node samples/transfer-check.js`





Expand Down
110 changes: 110 additions & 0 deletions samples/aws-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright 2022 Google LLC
*
* 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.
*/

'use strict';

async function main(
projectId,
description,
awsSourceBucket,
awsAccessKeyId,
awsSecretAccessKey,
gcsSinkBucket
) {
// [START storagetransfer_transfer_from_aws]

// Imports the Google Cloud client library
const {
StorageTransferServiceClient,
} = require('@google-cloud/storage-transfer');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of the Google Cloud Platform Project that owns the job
// projectId = 'my-project-id'

// A useful description for your transfer job
// description = 'My transfer job'

// AWS S3 source bucket name
// awsSourceBucket = 'my-s3-source-bucket'

// AWS Access Key ID
// awsAccessKeyId = 'AKIA...'

// AWS Secret Access Key
// awsSecretAccessKey = 'HEAoMK2.../...ku8'

// Google Cloud Storage destination bucket name
// gcsSinkBucket = 'my-gcs-destination-bucket'

// Creates a client
const client = new StorageTransferServiceClient();

/**
* Creates a one-time transfer job from Amazon S3 to Google Cloud Storage.
*/
async function transferFromS3() {
// Setting the start date and the end date as the same time creates a
// one-time transfer
const now = new Date();
const oneTimeSchedule = {
day: now.getDate(),
month: now.getMonth() + 1,
year: now.getFullYear(),
};

// Runs the request and creates the job
const [transferJob] = await client.createTransferJob({
transferJob: {
projectId,
description,
status: 'ENABLED',
schedule: {
scheduleStartDate: oneTimeSchedule,
scheduleEndDate: oneTimeSchedule,
},
transferSpec: {
awsS3DataSource: {
bucketName: awsSourceBucket,
awsAccessKey: {
accessKeyId: awsAccessKeyId,
secretAccessKey: awsSecretAccessKey,
},
},
gcsDataSink: {
bucketName: gcsSinkBucket,
},
},
},
});

console.log(
`Created and ran a transfer job from '${awsSourceBucket}' to '${gcsSinkBucket}' with name ${transferJob.name}`
);
}

transferFromS3();
// [END storagetransfer_transfer_from_aws]
}

main(...process.argv.slice(2));

process.on('unhandledRejection', err => {
console.error(err);
process.exitCode = 1;
});
67 changes: 67 additions & 0 deletions samples/check-latest-transfer-operation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2022 Google LLC
*
* 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.
*/

'use strict';

async function main(projectId, jobName) {
// [START storagetransfer_get_latest_transfer_operation]

// Imports the Google Cloud client library
const {
StorageTransferServiceClient,
} = require('@google-cloud/storage-transfer');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of the Google Cloud Platform Project that owns the job
// projectId = 'my-project-id'

// Storage Transfer Service job name
// jobName = 'transferJobs/1234567890'

// Creates a client
const client = new StorageTransferServiceClient();

/**
* Checks the latest transfer operation for a given transfer job.
*/
async function checkLatestTransferOperation() {
const [transferJob] = await client.getTransferJob({projectId, jobName});

if (transferJob.latestOperationName) {
const [transferOperation] = await client.operationsClient.getOperation({
name: transferJob.latestOperationName,
});

const operation = JSON.stringify(transferOperation, null, 2);

console.log(`Latest transfer operation for '${jobName}': ${operation}`);
} else {
console.log(`Transfer job '${jobName}' has not ran yet.`);
}
}

checkLatestTransferOperation();
// [END storagetransfer_get_latest_transfer_operation]
}

main(...process.argv.slice(2));

process.on('unhandledRejection', err => {
console.error(err);
process.exitCode = 1;
});
Loading