Skip to content

Commit e755f7d

Browse files
authored
feat: add storage control folder and managed folder samples (GoogleCloudPlatform#3761)
* feat: add storage control folder and managed folder samples * add tests * add storage-control.yaml
1 parent 0beddbd commit e755f7d

14 files changed

+812
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: storage-control
16+
on:
17+
push:
18+
branches:
19+
- main
20+
paths:
21+
- 'storage-control/**'
22+
- '.github/workflows/storage-control.yaml'
23+
- '.github/workflows/test.yaml'
24+
pull_request:
25+
types:
26+
- opened
27+
- reopened
28+
- synchronize
29+
- labeled
30+
paths:
31+
- 'storage-control/**'
32+
- '.github/workflows/storage-control.yaml'
33+
- '.github/workflows/test.yaml'
34+
schedule:
35+
- cron: '0 0 * * 0'
36+
jobs:
37+
test:
38+
permissions:
39+
contents: 'read'
40+
id-token: 'write'
41+
if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run'
42+
uses: ./.github/workflows/test.yaml
43+
with:
44+
name: 'storage-control'
45+
path: 'storage-control'
46+
flakybot:
47+
permissions:
48+
contents: 'read'
49+
id-token: 'write'
50+
if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail
51+
uses: ./.github/workflows/flakybot.yaml
52+
needs: [test]

storage-control/createFolder.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(bucketName, folderName) {
18+
// [START storage_control_create_folder]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
23+
// The name of your GCS bucket
24+
// const bucketName = 'bucketName';
25+
26+
// The name of the folder to be created
27+
// const folderName = 'folderName';
28+
29+
// Imports the Control library
30+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
31+
32+
// Instantiates a client
33+
const controlClient = new StorageControlClient();
34+
35+
async function callCreateFolder() {
36+
const bucketPath = controlClient.bucketPath('_', bucketName);
37+
38+
// Create the request
39+
const request = {
40+
parent: bucketPath,
41+
folderId: folderName,
42+
};
43+
44+
// Run request
45+
const [response] = await controlClient.createFolder(request);
46+
console.log(`Created folder: ${response.name}.`);
47+
}
48+
49+
callCreateFolder();
50+
// [END storage_control_create_folder]
51+
}
52+
53+
process.on('unhandledRejection', err => {
54+
console.error(err.message);
55+
process.exitCode = 1;
56+
});
57+
main(...process.argv.slice(2));
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(bucketName, managedFolderName) {
18+
// [START storage_control_managed_folder_create]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
23+
// The name of your GCS bucket
24+
// const bucketName = 'bucketName';
25+
26+
// The name of the managed folder to be created
27+
// const managedFolderName = 'managedFolderName';
28+
29+
// Imports the Control library
30+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
31+
32+
// Instantiates a client
33+
const controlClient = new StorageControlClient();
34+
35+
async function callCreateManagedFolder() {
36+
const bucketPath = controlClient.bucketPath('_', bucketName);
37+
38+
// Create the request
39+
const request = {
40+
parent: bucketPath,
41+
managedFolderId: managedFolderName,
42+
};
43+
44+
// Run request
45+
const [response] = await controlClient.createManagedFolder(request);
46+
console.log(`Created managed folder: ${response.name}.`);
47+
}
48+
49+
callCreateManagedFolder();
50+
// [END storage_control_managed_folder_create]
51+
}
52+
53+
process.on('unhandledRejection', err => {
54+
console.error(err.message);
55+
process.exitCode = 1;
56+
});
57+
main(...process.argv.slice(2));

storage-control/deleteFolder.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(bucketName, folderName) {
18+
// [START storage_control_delete_folder]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
23+
// The name of your GCS bucket
24+
// const bucketName = 'bucketName';
25+
26+
// The name of the folder to be deleted
27+
// const folderName = 'folderName';
28+
29+
// Imports the Control library
30+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
31+
32+
// Instantiates a client
33+
const controlClient = new StorageControlClient();
34+
35+
async function callDeleteFolder() {
36+
const folderPath = controlClient.folderPath('_', bucketName, folderName);
37+
38+
// Create the request
39+
const request = {
40+
name: folderPath,
41+
};
42+
43+
// Run request
44+
await controlClient.deleteFolder(request);
45+
console.log(`Deleted folder: ${folderName}.`);
46+
}
47+
48+
callDeleteFolder();
49+
// [END storage_control_delete_folder]
50+
}
51+
52+
process.on('unhandledRejection', err => {
53+
console.error(err.message);
54+
process.exitCode = 1;
55+
});
56+
main(...process.argv.slice(2));
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(bucketName, managedFolderName) {
18+
// [START storage_control_managed_folder_delete]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
23+
// The name of your GCS bucket
24+
// const bucketName = 'bucketName';
25+
26+
// The name of the folder to be deleted
27+
// const managedFolderName = 'managedFolderName';
28+
29+
// Imports the Control library
30+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
31+
32+
// Instantiates a client
33+
const controlClient = new StorageControlClient();
34+
35+
async function callDeleteManagedFolder() {
36+
const managedFolderPath = controlClient.managedFolderPath(
37+
'_',
38+
bucketName,
39+
managedFolderName
40+
);
41+
42+
// Create the request
43+
const request = {
44+
name: managedFolderPath,
45+
};
46+
47+
// Run request
48+
await controlClient.deleteManagedFolder(request);
49+
console.log(`Deleted managed folder: ${managedFolderName}.`);
50+
}
51+
52+
callDeleteManagedFolder();
53+
// [END storage_control_managed_folder_delete]
54+
}
55+
56+
process.on('unhandledRejection', err => {
57+
console.error(err.message);
58+
process.exitCode = 1;
59+
});
60+
main(...process.argv.slice(2));

storage-control/getFolder.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(bucketName, folderName) {
18+
// [START storage_control_get_folder]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
23+
// The name of your GCS bucket
24+
// const bucketName = 'bucketName';
25+
26+
// The name of the folder to get
27+
// const folderName = 'folderName';
28+
29+
// Imports the Control library
30+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
31+
32+
// Instantiates a client
33+
const controlClient = new StorageControlClient();
34+
35+
async function callGetFolder() {
36+
const folderPath = controlClient.folderPath('_', bucketName, folderName);
37+
38+
// Create the request
39+
const request = {
40+
name: folderPath,
41+
};
42+
43+
// Run request
44+
const [response] = await controlClient.getFolder(request);
45+
console.log(`Got folder: ${response.name}.`);
46+
}
47+
48+
callGetFolder();
49+
// [END storage_control_get_folder]
50+
}
51+
52+
process.on('unhandledRejection', err => {
53+
console.error(err.message);
54+
process.exitCode = 1;
55+
});
56+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)