-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
56 lines (41 loc) · 1.28 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const { GoogleAuth, OAuth2Client, Impersonated, IdTokenClient } = require('google-auth-library');
const { Storage } = require('@google-cloud/storage');
async function main() {
const targetPrincipal = "urlsigner@fabled-ray-104117.iam.gserviceaccount.com";
const bucketName = "some-bucket";
let projectId = 'fabled-ray-104117'
fileName = "file.txt"
const scopes = 'https://www.googleapis.com/auth/cloud-platform'
// get source credentials
const auth = new GoogleAuth({
scopes: scopes
});
const client = await auth.getClient();
// First impersonate
let targetClient = new Impersonated({
sourceClient: client,
targetPrincipal: targetPrincipal,
lifetime: 30,
delegates: [],
targetScopes: [scopes]
});
// use the impersonated creds to access gcs
const storageOptions = {
projectId,
authClient: targetClient,
};
const storage = new Storage(storageOptions);
// ####### GCS SignedURL
// pending https://github.com/googleapis/google-auth-library-nodejs/issues/1443
const options = {
version: 'v4',
action: 'read',
expires: Date.now() + 15 * 60 * 1000, // 15 minutes
};
const su = await storage
.bucket(bucketName)
.file('foo.txt')
.getSignedUrl(options);
console.log(su);
}
main().catch(console.error);