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

feat(samples): Add POSIX & Manifest samples #67

Merged
merged 28 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7ace33d
feat(samples): Add POSIX samples
d-goog May 26, 2022
612b850
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] May 26, 2022
7cc13dd
Fix: `fs.promises` import
d-goog May 26, 2022
8e4e6d1
Merge branch 'posix-samples' of https://github.com/googleapis/nodejs-…
d-goog May 26, 2022
e1696d1
fix: clean-up created transfer job
d-goog May 26, 2022
0951983
chore: Add (safe) debug log
d-goog May 26, 2022
4a47cd5
fix: misc bugs
d-goog May 26, 2022
6236e9e
chore: remove debug log
d-goog May 27, 2022
86301e0
feat: add POSIX to POSIX sample
d-goog May 27, 2022
1946b0c
chore: typo & clean-up
d-goog May 27, 2022
7215ff6
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] May 27, 2022
cbf6aae
refactor: styling
d-goog Jun 1, 2022
28308ed
Merge branch 'posix-samples' of https://github.com/googleapis/nodejs-…
d-goog Jun 2, 2022
ba86cae
feat: Add POSIX Download sample
d-goog Jun 3, 2022
d613f20
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jun 3, 2022
9b08257
fix: Add ending '/'
d-goog Jun 3, 2022
dc22f57
style: shorten variable
d-goog Jun 3, 2022
bc80220
feat: Transfer Manifest request
d-goog Jun 14, 2022
ab5dafa
fix: typo
d-goog Jun 14, 2022
de8d929
Merge branch 'main' of https://github.com/googleapis/nodejs-storage-t…
d-goog Jun 14, 2022
28bb7cf
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jun 14, 2022
12dc353
Merge branch 'posix-samples' of https://github.com/googleapis/nodejs-…
gcf-owl-bot[bot] Jun 14, 2022
9b199ac
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jun 14, 2022
87c8161
Merge branch 'posix-samples' of https://github.com/googleapis/nodejs-…
gcf-owl-bot[bot] Jun 14, 2022
233441c
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Jun 14, 2022
56be733
Merge branch 'posix-samples' of https://github.com/googleapis/nodejs-…
gcf-owl-bot[bot] Jun 14, 2022
87c90a2
fix: `replaceAll` (not available in Node 12) -> `replace`
d-goog Jun 14, 2022
b7a99ca
Merge branch 'posix-samples' of https://github.com/googleapis/nodejs-…
d-goog Jun 14, 2022
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage-tra
| 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) |
| Manifest-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/manifest-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/manifest-request.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) |
| Posix-download | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-download.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/posix-download.js,samples/README.md) |
| Posix-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-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/posix-request.js,samples/README.md) |
| Posix-to-posix-request | [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-to-posix-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/posix-to-posix-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
72 changes: 72 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
* [Aws-request](#aws-request)
* [Check-latest-transfer-operation](#check-latest-transfer-operation)
* [Get-transfer-job-with-retries](#get-transfer-job-with-retries)
* [Manifest-request](#manifest-request)
* [Nearline-request](#nearline-request)
* [Posix-download](#posix-download)
* [Posix-request](#posix-request)
* [Posix-to-posix-request](#posix-to-posix-request)
* [Quickstart](#quickstart)
* [Transfer-check](#transfer-check)

Expand Down Expand Up @@ -85,6 +89,23 @@ __Usage:__



### Manifest-request

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/manifest-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/manifest-request.js,samples/README.md)

__Usage:__


`node samples/manifest-request.js`


-----




### Nearline-request

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/nearline-request.js).
Expand All @@ -102,6 +123,57 @@ __Usage:__



### Posix-download

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-download.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/posix-download.js,samples/README.md)

__Usage:__


`node samples/posix-download.js`


-----




### Posix-request

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-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/posix-request.js,samples/README.md)

__Usage:__


`node samples/posix-request.js`


-----




### Posix-to-posix-request

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/posix-to-posix-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/posix-to-posix-request.js,samples/README.md)

__Usage:__


`node samples/posix-to-posix-request.js`


-----




### Quickstart

View the [source code](https://github.com/googleapis/nodejs-storage-transfer/blob/main/samples/quickstart.js).
Expand Down
99 changes: 99 additions & 0 deletions samples/manifest-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* 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 = 'my-project',
sourceAgentPoolName = '',
rootDirectory = '',
gcsSinkBucket = '',
manifestLocation = ''
) {
// [START storagetransfer_manifest_request]

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

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// Your project id
// const projectId = 'my-project'

// The agent pool associated with the POSIX data source. Defaults to the default agent
// const sourceAgentPoolName = 'projects/my-project/agentPools/transfer_service_default'

// The root directory path on the source filesystem
// const rootDirectory = '/directory/to/transfer/source',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing comma here


// The ID of the GCS bucket to transfer data to
// const gcsSinkBucket = 'my-sink-bucket'

// Transfer manifest location. Must be a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must be a what?

// const manifestLocation = 'gs://my-bucket/sample_manifest.csv'

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

/**
* Creates a request to transfer from the local file system to the sink bucket
*/
async function transferDirectory() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "transferDirectory" the right name for this function? This only transfers the files specified in the manifest right? which may not be the entire directory if i understand it correctly, and may in fact include files from multiple directories within the root directory

const createRequest = {
transferJob: {
projectId,
transferSpec: {
sourceAgentPoolName,
posixDataSource: {
rootDirectory,
},
gcsDataSink: {bucketName: gcsSinkBucket},
transferManifest: {
location: manifestLocation,
},
},
status: 'ENABLED',
},
};

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

const runRequest = {
jobName: transferJob.name,
projectId: projectId,
};

await client.runTransferJob(runRequest);

console.log(
`Created and ran a transfer job from '${rootDirectory}' to '${gcsSinkBucket}' using manifest \`${manifestLocation}\` with name ${transferJob.name}`
);
}

transferDirectory();
// [END storagetransfer_manifest_request]
}

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

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
99 changes: 99 additions & 0 deletions samples/posix-download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* 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 = 'my-project',
sinkAgentPoolName = '',
gcsSourceBucket = '',
gcsSourcePath = '',
rootDirectory = ''
) {
// [START storagetransfer_download_to_posix]

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

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// Your project id
// const projectId = 'my-project'

// The agent pool associated with the POSIX data sink. Defaults to the default agent
// const sinkAgentPoolName = 'projects/my-project/agentPools/transfer_service_default'

// Google Cloud Storage source bucket name
// const gcsSourceBucket = 'my-gcs-source-bucket'

// An optional path on the Google Cloud Storage bucket to download from
// const gcsSourcePath = 'foo/bar/'

// The root directory path on the source filesystem
// const rootDirectory = '/directory/to/transfer/source',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing comma


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

/**
* Creates a request to transfer from the local file system to the sink bucket
*/
async function transferDirectory() {
const createRequest = {
transferJob: {
projectId,
transferSpec: {
sinkAgentPoolName,
gcsDataSource: {
bucketName: gcsSourceBucket,
path: gcsSourcePath,
},
posixDataSink: {
rootDirectory,
},
},
status: 'ENABLED',
},
};

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

const runRequest = {
jobName: transferJob.name,
projectId: projectId,
};

await client.runTransferJob(runRequest);

console.log(
`Downloading from '${gcsSourceBucket}' (path: \`${gcsSourcePath}\`) to '${rootDirectory}' with name ${transferJob.name}`
);
}

transferDirectory();
// [END storagetransfer_download_to_posix]
}

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

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
92 changes: 92 additions & 0 deletions samples/posix-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* 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 = 'my-project',
sourceAgentPoolName = '',
rootDirectory = '',
gcsSinkBucket = ''
) {
// [START storagetransfer_transfer_from_posix]

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

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// Your project id
// const projectId = 'my-project'

// The agent pool associated with the POSIX data source. Defaults to the default agent
// const sourceAgentPoolName = 'projects/my-project/agentPools/transfer_service_default'

// The root directory path on the source filesystem
// const rootDirectory = '/directory/to/transfer/source',

// The ID of the GCS bucket to transfer data to
// const gcsSinkBucket = 'my-sink-bucket'

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

/**
* Creates a request to transfer from the local file system to the sink bucket
*/
async function transferDirectory() {
const createRequest = {
transferJob: {
projectId,
transferSpec: {
sourceAgentPoolName,
posixDataSource: {
rootDirectory,
},
gcsDataSink: {bucketName: gcsSinkBucket},
},
status: 'ENABLED',
},
};

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

const runRequest = {
jobName: transferJob.name,
projectId: projectId,
};

await client.runTransferJob(runRequest);

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

transferDirectory();
// [END storagetransfer_transfer_from_posix]
}

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

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