Skip to content
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

feat(storage transfer): Added samples for storage transfer #2059

Merged
merged 24 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
43ed44d
quickstart and latest transfer check
thiyaguk09 Oct 11, 2024
3a999b6
added samples for nearline and combine test cases
thiyaguk09 Oct 15, 2024
a24fbc8
Merge pull request #1 from thiyaguk09/missing_transfer_service_samples
thiyaguk09 Oct 16, 2024
09bef5e
added samples for manifest and test cases
thiyaguk09 Oct 16, 2024
0756ae1
added samples and test cases for posix to GCS
thiyaguk09 Oct 16, 2024
5a9b2e8
fixes
thiyaguk09 Oct 17, 2024
21b22ce
variables fixes
thiyaguk09 Oct 17, 2024
83cd2ef
added samples and test cases for posix to posix
thiyaguk09 Oct 17, 2024
2c22adf
added samples and test cases for GCS to posix
thiyaguk09 Oct 17, 2024
285330d
Merge pull request #2 from thiyaguk09/sample_manifest
thiyaguk09 Oct 21, 2024
646298b
Merge branch 'GoogleCloudPlatform:main' into main
thiyaguk09 Oct 21, 2024
a32f6ad
Merge pull request #3 from thiyaguk09/sample_posix
thiyaguk09 Oct 21, 2024
f8a30e0
Merge branch 'GoogleCloudPlatform:main' into sample_transfer_between_…
thiyaguk09 Oct 21, 2024
9ece009
Merge pull request #4 from thiyaguk09/sample_transfer_between_posix
thiyaguk09 Oct 29, 2024
fc988fd
resolve conflict
thiyaguk09 Oct 31, 2024
067a654
Merge branch 'main' into sample_download_to_posix
thiyaguk09 Oct 31, 2024
d10c311
Merge pull request #5 from thiyaguk09/sample_download_to_posix
thiyaguk09 Oct 31, 2024
55ba8e3
added typehints
thiyaguk09 Nov 4, 2024
57d3b6e
added samples and test cases for event driven GCS transfer
thiyaguk09 Nov 6, 2024
b58b9fb
Merge pull request #6 from thiyaguk09/sample_event_driven_gcs
thiyaguk09 Nov 8, 2024
61f4ac5
lint fix
thiyaguk09 Nov 18, 2024
232bfa3
Merge branch 'main' into main
danielduhh Nov 18, 2024
4db5442
Merge branch 'main' into main
bshaffer Dec 13, 2024
2248b46
use latest library version
bshaffer Dec 13, 2024
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
75 changes: 75 additions & 0 deletions storagetransfer/src/posix_download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright 2024 Google Inc.
*
* 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.
*/

namespace Google\Cloud\Samples\StorageTransfer;

# [START storagetransfer_download_to_posix]

use Google\Cloud\StorageTransfer\V1\Client\StorageTransferServiceClient;
use Google\Cloud\StorageTransfer\V1\CreateTransferJobRequest;
use Google\Cloud\StorageTransfer\V1\GcsData;
use Google\Cloud\StorageTransfer\V1\PosixFilesystem;
use Google\Cloud\StorageTransfer\V1\RunTransferJobRequest;
use Google\Cloud\StorageTransfer\V1\TransferJob;
use Google\Cloud\StorageTransfer\V1\TransferJob\Status;
use Google\Cloud\StorageTransfer\V1\TransferSpec;

/**
* Creates a request to transfer from the local file system to the sink bucket
*
* @param string $projectId Your Google Cloud project ID.
* @param string $sinkAgentPoolName The agent pool associated with the POSIX data sink. Defaults to the default agent
* @param string $gcsSourceBucket Google Cloud Storage source bucket name.
* @param string $gcsSourcePath An optional path on the Google Cloud Storage bucket to download from.
* @param string $rootDirectory The root directory path on the destination filesystem.
*/
function posix_download($projectId, $sinkAgentPoolName, $gcsSourceBucket, $gcsSourcePath, $rootDirectory)
{
thiyaguk09 marked this conversation as resolved.
Show resolved Hide resolved
// $project = 'my-project-id';
// $sinkAgentPoolName = 'projects/my-project/agentPools/transfer_service_default';
// $gcsSourceBucket = 'my-gcs-source-bucket';
// $gcsSourcePath = 'foo/bar/';
// $rootDirectory = '/directory/to/transfer/source';
$transferJob = new TransferJob([
'project_id' => $projectId,
'transfer_spec' => new TransferSpec([
'sink_agent_pool_name' => $sinkAgentPoolName,
'gcs_data_source' => new GcsData([
'bucket_name' => $gcsSourceBucket,
'path' => $gcsSourcePath
]),
'posix_data_sink' => new PosixFilesystem(['root_directory' => $rootDirectory])
]),
'status' => Status::ENABLED
]);

$client = new StorageTransferServiceClient();
$createRequest = (new CreateTransferJobRequest())
->setTransferJob($transferJob);
$response = $client->createTransferJob($createRequest);
$runRequest = (new RunTransferJobRequest())
->setJobName($response->getName())
->setProjectId($projectId);
$client->runTransferJob($runRequest);

printf('Created and ran a transfer job from %s to %s with name %s ' . PHP_EOL, $gcsSourcePath, $rootDirectory, $response->getName());
}
# [END storagetransfer_download_to_posix]

// The following 2 lines are only needed to run the samples
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
35 changes: 35 additions & 0 deletions storagetransfer/test/StorageTransferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,41 @@ public function testPosixToPosixRequest()
}
}

public function testDownloadToPosix()
{
try {
$tempFileName = 'text.txt';
$sinkAgentPoolName = '';
$rootDirectory = self::$root . '/sts-download-to-posix-test';
$gcsSourcePath = 'sts-manifest-request-test/';
if (!is_dir($rootDirectory)) {
mkdir($rootDirectory, 0700, true);
}
$tempFile = $rootDirectory . '/' . $tempFileName;
file_put_contents($tempFile, 'test data');

// Upload the temporary file to GCS
self::$sourceBucket->upload(
fopen($tempFile, 'r'),
[
'name' => $tempFileName
]
);

$output = $this->runFunctionSnippet('posix_download', [
self::$projectId, $sinkAgentPoolName, self::$sourceBucket->name(), $gcsSourcePath, $rootDirectory
]);

$this->assertMatchesRegularExpression('/transferJobs\/.*/', $output);
} finally {
unlink($tempFile);
rmdir($rootDirectory);
self::$sourceBucket->object($tempFileName)->delete();
preg_match('/transferJobs\/\w+/', $output, $match);
self::deleteTransferJob($match[0]);
}
}

// deletes a transfer job created by a sample to clean up
private static function deleteTransferJob($jobName)
{
Expand Down
Loading