Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.
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
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,19 @@
"storage"
],
"scripts": {
"codecov": "nyc report --reporter=json && codecov -f .coverage/*.json",
"docs": "jsdoc -c .jsdoc.js",
"generate-scaffolding": "repo-tools generate all && repo-tools generate lib_samples_readme -l samples/ --config ../.cloud-repo-tools.json",
"system-test": "mocha build/system-test --timeout 600000",
"presystem-test": "npm run compile",
"test-only": "nyc mocha build/test",
"test": "npm run test-only",
"pretest-only": "npm run compile",
"test": "nyc mocha build/test",
"pretest": "npm run compile",
"lint": "eslint samples/ && gts check",
"samples-test": "npm link && cd samples/ && npm link ../ && npm test && cd ../",
"all-test": "npm test && npm run system-test && npm run samples-test",
"check": "gts check",
"clean": "gts clean",
"compile": "tsc -p .",
"fix": "gts fix && eslint --fix samples/**/*.js",
"fix": "gts fix && eslint --fix '**/*.js'",
"prepare": "npm run compile"
},
"dependencies": {
Expand Down
23 changes: 8 additions & 15 deletions samples/package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
{
"name": "@google-cloud/storage-samples",
"description": "Samples for the Cloud Storage Client Library for Node.js.",
"version": "0.0.1",
"license": "Apache-2.0",
"author": "Google Inc.",
"engines": {
"node": ">=8"
},
"repository": "googleapis/nodejs-storage",
"private": true,
"nyc": {
"exclude": [
"**/*.test.js"
]
},
"files": [
"*.js"
],
"scripts": {
"cleanup": "node scripts/cleanup",
"mocha": "mocha system-test/*.js --timeout 600000",
"cover": "nyc --reporter=lcov --cache mocha system-test/*.js --timeout 600000 && nyc report",
"test": "npm run cover"
"test": "mocha system-test/*.js --timeout 600000"
},
"dependencies": {
"@google-cloud/pubsub": "*",
"@google-cloud/pubsub": "^0.22.2",
"@google-cloud/storage": "^2.3.3",
"uuid": "^3.3.2",
"yargs": "^12.0.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"mocha": "^5.2.0",
"nyc": "^13.0.0",
"proxyquire": "^2.0.1",
"sinon": "^7.0.0"
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^5.2.0"
}
}
32 changes: 12 additions & 20 deletions samples/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,21 @@
'use strict';

// [START storage_quickstart]
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
async function quickstart(
projectId = 'YOUR_PROJECT_ID', // Your Google Cloud Platform project ID
bucketName = 'my-new-bucket' // The name for the new bucket
) {
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';
// Creates a client
const storage = new Storage({projectId});

// Creates a client
const storage = new Storage({
projectId: projectId,
});

// The name for the new bucket
const bucketName = 'my-new-bucket';

// Creates the new bucket
async function createBucket() {
// Creates the new bucket
await storage.createBucket(bucketName);
console.log(`Bucket ${bucketName} created.`);
}

try {
createBucket();
} catch (err) {
console.error('ERROR:', err);
}
// [END storage_quickstart]

const args = process.argv.slice(2);
quickstart(...args).catch(console.error);
5 changes: 1 addition & 4 deletions samples/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
---
env:
mocha : true
rules:
node/no-unpublished-require: off
no-empty: off
mocha : true
194 changes: 73 additions & 121 deletions samples/system-test/acl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,179 +15,131 @@

'use strict';

const path = require(`path`);
const {Storage} = require(`@google-cloud/storage`);
const assert = require('assert');
const tools = require(`@google-cloud/nodejs-repo-tools`);
const uuid = require(`uuid`);
const {Storage} = require('@google-cloud/storage');
const {assert} = require('chai');
const execa = require('execa');
const uuid = require('uuid');
const path = require('path');

const storage = new Storage();

const cwd = path.join(__dirname, `..`);
const bucketName = `nodejs-storage-samples-${uuid.v4()}`;
const bucket = storage.bucket(bucketName);
const userEmail = `jdobry@google.com`;
const fileName = `test.txt`;
const filePath = path.join(__dirname, `../resources`, fileName);
const cmd = `node acl.js`;
const userEmail = 'jdobry@google.com';
const fileName = 'test.txt';
const filePath = path.join(__dirname, '..', 'resources', fileName);
const cmd = 'node acl.js';

const exec = async cmd => (await execa.shell(cmd)).stdout;

before(tools.checkCredentials);
before(async () => {
await bucket.create();
await bucket.upload(filePath);
});

after(async () => {
// Try deleting all files twice
try {
await bucket.deleteFiles({force: true});
} catch (err) {} // ignore error
} catch (err) {
// ignore error
}
try {
await bucket.deleteFiles({force: true});
} catch (err) {} // ignore error
} catch (err) {
// ignore error
}
try {
await bucket.delete();
} catch (err) {} // ignore error
} catch (err) {
// ignore error
}
});

it(`should print acl for a bucket`, async () => {
const results = await tools.runAsyncWithIO(
`${cmd} print-bucket-acl ${bucketName}`,
cwd
);
assert.strictEqual(
(results.stdout + results.stderr).includes(`OWNER: project-editors-`),
true
);
assert.strictEqual(
(results.stdout + results.stderr).includes(`OWNER: project-owners-`),
true
);
assert.strictEqual(
(results.stdout + results.stderr).includes(`READER: project-viewers-`),
true
);
it('should print acl for a bucket', async () => {
const out = await exec(`${cmd} print-bucket-acl ${bucketName}`);
assert.match(out, /OWNER: project-editors-/);
assert.match(out, /OWNER: project-owners-/);
assert.match(out, /READER: project-viewers-/);
});

it(`should print acl for a file`, async () => {
const results = await tools.runAsyncWithIO(
`${cmd} print-file-acl ${bucketName} ${fileName}`,
cwd
);
assert.strictEqual(
(results.stdout + results.stderr).includes(`OWNER: project-editors-`),
true
);
assert.strictEqual(
(results.stdout + results.stderr).includes(`OWNER: project-owners-`),
true
);
assert.strictEqual(
(results.stdout + results.stderr).includes(`READER: project-viewers-`),
true
);
it('should print acl for a file', async () => {
const out = await exec(`${cmd} print-file-acl ${bucketName} ${fileName}`);
assert.match(out, /OWNER: project-editors-/);
assert.match(out, /OWNER: project-owners-/);
assert.match(out, /READER: project-viewers-/);
});

it(`should print a user's acl for a bucket`, async () => {
it('should print a users acl for a bucket', async () => {
await bucket.acl.readers.addUser(userEmail);
const results = await tools.runAsyncWithIO(
`${cmd} print-bucket-acl-for-user ${bucketName} ${userEmail}`,
cwd
);
assert.strictEqual(
(results.stdout + results.stderr).includes(`READER: user-${userEmail}`),
true
const out = await exec(
`${cmd} print-bucket-acl-for-user ${bucketName} ${userEmail}`
);
assert.match(out, new RegExp(`READER: user-${userEmail}`));
await bucket.acl.readers.deleteUser(userEmail);
});

it(`should add a user as an owner on a bucket`, async () => {
const results = await tools.runAsyncWithIO(
`${cmd} add-bucket-owner ${bucketName} ${userEmail}`,
cwd
);
assert.strictEqual(
(results.stdout + results.stderr).includes(
`Added user ${userEmail} as an owner on bucket ${bucketName}.`
),
true
it('should add a user as an owner on a bucket', async () => {
const out = await exec(`${cmd} add-bucket-owner ${bucketName} ${userEmail}`);
assert.match(
out,
new RegExp(`Added user ${userEmail} as an owner on bucket ${bucketName}.`)
);
});

it(`should remove a user from a bucket`, async () => {
const results = await tools.runAsyncWithIO(
`${cmd} remove-bucket-owner ${bucketName} ${userEmail}`,
cwd
it('should remove a user from a bucket', async () => {
const out = await exec(
`${cmd} remove-bucket-owner ${bucketName} ${userEmail}`
);
assert.strictEqual(
(results.stdout + results.stderr).includes(
`Removed user ${userEmail} from bucket ${bucketName}.`
),
true
assert.match(
out,
new RegExp(`Removed user ${userEmail} from bucket ${bucketName}.`)
);
});

it(`should add a user as a default owner on a bucket`, async () => {
const results = await tools.runAsyncWithIO(
`${cmd} add-bucket-default-owner ${bucketName} ${userEmail}`,
cwd
it('should add a user as a default owner on a bucket', async () => {
const out = await exec(
`${cmd} add-bucket-default-owner ${bucketName} ${userEmail}`
);
assert.strictEqual(
(results.stdout + results.stderr).includes(
`Added user ${userEmail} as an owner on bucket ${bucketName}.`
),
true
assert.match(
out,
new RegExp(`Added user ${userEmail} as an owner on bucket ${bucketName}.`)
);
});

it(`should remove a default user from a bucket`, async () => {
const results = await tools.runAsyncWithIO(
`${cmd} remove-bucket-default-owner ${bucketName} ${userEmail}`,
cwd
it('should remove a default user from a bucket', async () => {
const out = await exec(
`${cmd} remove-bucket-default-owner ${bucketName} ${userEmail}`
);
assert.strictEqual(
(results.stdout + results.stderr).includes(
`Removed user ${userEmail} from bucket ${bucketName}.`
),
true
assert.match(
out,
new RegExp(`Removed user ${userEmail} from bucket ${bucketName}.`)
);
});

it(`should print a user's acl for a file`, async () => {
it('should print a users acl for a file', async () => {
await bucket.file(fileName).acl.readers.addUser(userEmail);
const results = await tools.runAsyncWithIO(
`${cmd} print-file-acl-for-user ${bucketName} ${fileName} ${userEmail}`,
cwd
);
assert.strictEqual(
(results.stdout + results.stderr).includes(`READER: user-${userEmail}`),
true
const out = await exec(
`${cmd} print-file-acl-for-user ${bucketName} ${fileName} ${userEmail}`
);
assert.match(out, new RegExp(`READER: user-${userEmail}`));
await bucket.file(fileName).acl.readers.deleteUser(userEmail);
});

it(`should add a user as an owner on a bucket`, async () => {
const results = await tools.runAsyncWithIO(
`${cmd} add-file-owner ${bucketName} ${fileName} ${userEmail}`,
cwd
it('should add a user as an owner on a bucket', async () => {
const out = await exec(
`${cmd} add-file-owner ${bucketName} ${fileName} ${userEmail}`
);
assert.strictEqual(
(results.stdout + results.stderr).includes(
`Added user ${userEmail} as an owner on file ${fileName}.`
),
true
assert.match(
out,
new RegExp(`Added user ${userEmail} as an owner on file ${fileName}.`)
);
});

it(`should remove a user from a bucket`, async () => {
const results = await tools.runAsyncWithIO(
`${cmd} remove-file-owner ${bucketName} ${fileName} ${userEmail}`,
cwd
it('should remove a user from a bucket', async () => {
const out = await exec(
`${cmd} remove-file-owner ${bucketName} ${fileName} ${userEmail}`
);
assert.strictEqual(
(results.stdout + results.stderr).includes(
`Removed user ${userEmail} from file ${fileName}.`
),
true
assert.match(
out,
new RegExp(`Removed user ${userEmail} from file ${fileName}.`)
);
});
Loading