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 11 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage-tra
| 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) |
| 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
36 changes: 36 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* [Check-latest-transfer-operation](#check-latest-transfer-operation)
* [Get-transfer-job-with-retries](#get-transfer-job-with-retries)
* [Nearline-request](#nearline-request)
* [Posix-request](#posix-request)
* [Posix-to-posix-request](#posix-to-posix-request)
* [Quickstart](#quickstart)
* [Transfer-check](#transfer-check)

Expand Down Expand Up @@ -102,6 +104,40 @@ __Usage:__



### 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
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;
});
106 changes: 106 additions & 0 deletions samples/posix-to-posix-request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* 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 = '',
sinkAgentPoolName = '',
rootDirectory = '',
destinationDirectory = '',
bucketName = ''
) {
// [START storagetransfer_transfer_posix_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 source. Defaults to the default agent
// const sourceAgentPoolName = 'projects/my-project/agentPools/transfer_service_default'

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

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

// The root directory path on the sink filesystem
// const destinationDirectory = '/directory/to/transfer/sink'

// The ID of the GCS bucket for intermediate storage
// const bucketName = 'my-intermediate-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,
sinkAgentPoolName,
posixDataSource: {
rootDirectory,
},
posixDataSink: {
rootDirectory: destinationDirectory,
},
gcsIntermediateDataLocation: {
bucketName,
},
},
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 '${destinationDirectory}' with name ${transferJob.name}`
);
}

transferDirectory();
// [END storagetransfer_transfer_posix_to_posix]
}

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

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
79 changes: 79 additions & 0 deletions samples/test/posix-request.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* 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';

const fs = require('fs').promises;
const os = require('os');
const path = require('path');

const {assert} = require('chai');
const {after, before, describe, it} = require('mocha');

const {BucketManager, TransferJobManager, runSample} = require('./utils');

describe('posix-request', () => {
const testBucketManager = new BucketManager();
const testTransferJobManager = new TransferJobManager();

let projectId;
let sourceAgentPoolName;
let rootDirectory;
let gcsSinkBucket;

let tempFile;

before(async () => {
projectId = await testTransferJobManager.client.getProjectId();

// Use default pool
sourceAgentPoolName = '';

rootDirectory = await fs.mkdtemp(
path.join(os.tmpdir(), 'sts-posix-request-test-src-')
);
tempFile = path.join(rootDirectory, 'text.txt');

await fs.writeFile(tempFile, 'test data');

gcsSinkBucket = (await testBucketManager.generateGCSBucket()).name;
});

after(async () => {
await testBucketManager.deleteBuckets();
await testTransferJobManager.cleanUp();
await fs.unlink(tempFile);
await fs.rmdir(rootDirectory);
});

it('should create a transfer job from POSIX to GCS', async () => {
const output = await runSample('posix-request', [
projectId,
sourceAgentPoolName,
rootDirectory,
gcsSinkBucket,
]);

// If it ran successfully and a job was created, delete it to clean up
const [jobName] = output.match(/transferJobs.*/);
if (jobName) {
testTransferJobManager.transferJobToCleanUp(jobName);
}

// Find at least 1 transfer operation from the transfer job in the output
assert.include(output, 'Created and ran a transfer job');
});
});
Loading