Skip to content

Commit 29d2ecf

Browse files
committed
fix(storage): standardize URL construction to use v1 API paths
1 parent b8a6548 commit 29d2ecf

18 files changed

Lines changed: 257 additions & 236 deletions

File tree

handwritten/storage/src/acl.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,10 @@ class Acl extends AclRoleAccessorMethods {
528528
if (this.parent instanceof File) {
529529
const file = this.parent as File;
530530
const bucket = file.parent;
531-
url = `${bucket.baseUrl}/${bucket.name}/${file.baseUrl}/${file.name}${url}`;
531+
url = `/storage/v1/b/${bucket.name}/o/${encodeURIComponent(file.name)}${url}`;
532532
} else if (this.parent instanceof Bucket) {
533533
const bucket = this.parent as Bucket;
534-
url = `${bucket.baseUrl}/${bucket.name}${url}`;
534+
url = `/storage/v1/b/${bucket.name}${url}`;
535535
}
536536

537537
this.storageTransport
@@ -644,14 +644,14 @@ class Acl extends AclRoleAccessorMethods {
644644
query.userProject = options.userProject;
645645
}
646646

647-
let url = `${this.pathPrefix}/${options.entity}`;
647+
let url = `${this.pathPrefix}/${encodeURIComponent(options.entity)}`;
648648
if (this.parent instanceof File) {
649649
const file = this.parent as File;
650650
const bucket = file.parent;
651-
url = `${bucket.baseUrl}/${bucket.name}/${file.baseUrl}/${file.name}${url}`;
651+
url = `/storage/v1/b/${bucket.name}/o/${encodeURIComponent(file.name)}${url}`;
652652
} else if (this.parent instanceof Bucket) {
653653
const bucket = this.parent as Bucket;
654-
url = `${bucket.baseUrl}/${bucket.name}${url}`;
654+
url = `/storage/v1/b/${bucket.name}${url}`;
655655
}
656656

657657
this.storageTransport
@@ -768,7 +768,7 @@ class Acl extends AclRoleAccessorMethods {
768768

769769
let url = `${this.pathPrefix}`;
770770
if (options) {
771-
url = `${url}/${options.entity}`;
771+
url = `${url}/${encodeURIComponent(options.entity)}`;
772772
if (options.generation) {
773773
query.generation = options.generation;
774774
}
@@ -781,10 +781,10 @@ class Acl extends AclRoleAccessorMethods {
781781
if (this.parent instanceof File) {
782782
const file = this.parent as File;
783783
const bucket = file.parent;
784-
url = `${bucket.baseUrl}/${bucket.name}/${file.baseUrl}/${file.name}${url}`;
784+
url = `/storage/v1/b/${bucket.name}/o/${encodeURIComponent(file.name)}${url}`;
785785
} else if (this.parent instanceof Bucket) {
786786
const bucket = this.parent as Bucket;
787-
url = `${bucket.baseUrl}/${bucket.name}${url}`;
787+
url = `/storage/v1/b/${bucket.name}${url}`;
788788
}
789789

790790
this.storageTransport
@@ -888,14 +888,14 @@ class Acl extends AclRoleAccessorMethods {
888888
query.userProject = options.userProject;
889889
}
890890

891-
let url = `${this.pathPrefix}/${options.entity}`;
891+
let url = `${this.pathPrefix}/${encodeURIComponent(options.entity)}`;
892892
if (this.parent instanceof File) {
893893
const file = this.parent as File;
894894
const bucket = file.parent;
895-
url = `${bucket.baseUrl}/${bucket.name}/${file.baseUrl}/${file.name}${url}`;
895+
url = `/storage/v1/b/${bucket.name}/o/${encodeURIComponent(file.name)}${url}`;
896896
} else if (this.parent instanceof Bucket) {
897897
const bucket = this.parent as Bucket;
898-
url = `${bucket.baseUrl}/${bucket.name}${url}`;
898+
url = `/storage/v1/b/${bucket.name}${url}`;
899899
}
900900

901901
this.storageTransport

handwritten/storage/src/bucket.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {
12721272
super({
12731273
storageTransport: storage.storageTransport,
12741274
parent: storage,
1275-
baseUrl: '/b',
1275+
baseUrl: '/storage/v1/b',
12761276
id: name,
12771277
createMethod: storage.createBucket.bind(storage),
12781278
methods,
@@ -1736,7 +1736,7 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {
17361736
.makeRequest(
17371737
{
17381738
method: 'POST',
1739-
url: '/compose',
1739+
url: `/storage/v1/b/${this.name}/o/${encodeURIComponent(destinationFile.name)}/compose`,
17401740
maxRetries,
17411741
body: JSON.stringify({
17421742
destination: {
@@ -1758,6 +1758,9 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {
17581758
return sourceObject;
17591759
}),
17601760
}),
1761+
headers: {
1762+
'Content-Type': 'application/json',
1763+
},
17611764
queryParameters: options as unknown as StorageQueryParameters,
17621765
},
17631766
(err, resp) => {
@@ -2099,6 +2102,9 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {
20992102
body: JSON.stringify(convertObjKeysToSnakeCase(body)),
21002103
queryParameters: query as unknown as StorageQueryParameters,
21012104
retry: false,
2105+
headers: {
2106+
'Content-Type': 'application/json',
2107+
},
21022108
},
21032109
(err, data, resp) => {
21042110
if (err) {

handwritten/storage/src/channel.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Channel extends ServiceObject<Channel, BaseMetadata> {
4343
const config = {
4444
parent: storage,
4545
storageTransport: storage.storageTransport,
46-
baseUrl: '/channels',
46+
baseUrl: '/storage/v1/channels',
4747
id: '',
4848
methods: {},
4949
};
@@ -89,6 +89,9 @@ class Channel extends ServiceObject<Channel, BaseMetadata> {
8989
method: 'POST',
9090
url: `${this.baseUrl}/stop`,
9191
body: JSON.stringify(this.metadata),
92+
headers: {
93+
'Content-Type': 'application/json',
94+
},
9295
responseType: 'json',
9396
},
9497
(err, data, resp) => {

handwritten/storage/src/file.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
SetMetadataOptions,
7474
} from './nodejs-common/service-object.js';
7575
import {
76+
Gaxios,
7677
GaxiosError,
7778
GaxiosInterceptor,
7879
GaxiosOptionsPrepared,
@@ -82,7 +83,6 @@ import {
8283
StorageQueryParameters,
8384
StorageRequestOptions,
8485
} from './storage-transport.js';
85-
import * as gaxios from 'gaxios';
8686
import mime from 'mime';
8787

8888
export type GetExpirationDateResponse = [Date];
@@ -1412,6 +1412,7 @@ class File extends ServiceObject<File, FileMetadata> {
14121412
} else if (newFile.kmsKeyName !== undefined) {
14131413
query.destinationKmsKeyName = newFile.kmsKeyName;
14141414
}
1415+
headers.set('Content-Type', 'application/json');
14151416

14161417
if (query.destinationKmsKeyName) {
14171418
this.kmsKeyName = query.destinationKmsKeyName;
@@ -1441,7 +1442,7 @@ class File extends ServiceObject<File, FileMetadata> {
14411442
.makeRequest<RewriteResponse>(
14421443
{
14431444
method: 'POST',
1444-
url: `/b/${this.bucket.name}/o/${encodeURIComponent(this.name)}/rewriteTo/b/${
1445+
url: `/storage/v1/b/${this.bucket.name}/o/${encodeURIComponent(this.name)}/rewriteTo/b/${
14451446
destBucket.name
14461447
}/o/${encodeURIComponent(newFile.name)}`,
14471448
queryParameters: query as unknown as StorageQueryParameters,
@@ -1730,7 +1731,7 @@ class File extends ServiceObject<File, FileMetadata> {
17301731
}
17311732

17321733
const reqOpts: StorageRequestOptions = {
1733-
url: `${this.bucket.baseUrl}/${this.bucket.name}${this.baseUrl}/${this.name}`,
1734+
url: `/storage/v1/b/${this.bucket.name}/o/${encodeURIComponent(this.name)}`,
17341735
headers,
17351736
queryParameters: query as unknown as StorageQueryParameters,
17361737
responseType: 'stream',
@@ -3309,22 +3310,18 @@ class File extends ServiceObject<File, FileMetadata> {
33093310
*/
33103311

33113312
isPublic(callback?: IsPublicCallback): Promise<IsPublicResponse> | void {
3312-
// Build any custom headers based on the defined interceptors on the parent
3313-
// storage object and this object
3314-
const storageInterceptors = this.storage?.interceptors || [];
3315-
const fileInterceptors = this.interceptors || [];
3316-
const allInterceptors = storageInterceptors.concat(fileInterceptors);
3317-
3318-
for (const curInter of allInterceptors) {
3319-
gaxios.instance.interceptors.request.add(curInter);
3320-
}
3313+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3314+
const {callback: cb} = normalize<any, IsPublicCallback>(
3315+
undefined,
3316+
callback,
3317+
);
3318+
const url = `${this.storage.apiEndpoint}/${this.bucket.name}/${encodeURIComponent(this.name)}`;
33213319

3320+
const gaxios = new Gaxios();
33223321
gaxios
33233322
.request({
33243323
method: 'GET',
3325-
url: `${this.storage.apiEndpoint}/${
3326-
this.bucket.name
3327-
}/${encodeURIComponent(this.name)}`,
3324+
url,
33283325
retryConfig: {
33293326
retry: this.storage.retryOptions.maxRetries,
33303327
noResponseRetries: this.storage.retryOptions.maxRetries,
@@ -3334,12 +3331,17 @@ class File extends ServiceObject<File, FileMetadata> {
33343331
totalTimeout: this.storage.retryOptions.totalTimeout,
33353332
},
33363333
})
3337-
.then(() => callback!(null, true))
3334+
.then(() => {
3335+
cb(null, true);
3336+
})
33383337
.catch(err => {
3339-
if (err.status === 403) {
3340-
callback!(null, false);
3338+
const status = err.response?.status;
3339+
// 401 Unauthorized or 403 Forbidden means the object is NOT public.
3340+
if (status === 401 || status === 403) {
3341+
cb(null, false);
33413342
} else {
3342-
callback!(err);
3343+
// Any other error (like 404) is a real error.
3344+
cb(err);
33433345
}
33443346
});
33453347
}
@@ -3687,7 +3689,7 @@ class File extends ServiceObject<File, FileMetadata> {
36873689
.makeRequest(
36883690
{
36893691
method: 'POST',
3690-
url: `/b/${this.bucket.name}/o/${encodeURIComponent(this.name)}/moveTo/o/${encodeURIComponent(newFile.name)}`,
3692+
url: `/storage/v1/b/${this.bucket.name}/o/${encodeURIComponent(this.name)}/moveTo/o/${encodeURIComponent(newFile.name)}`,
36913693
queryParameters: query as StorageQueryParameters,
36923694
body: JSON.stringify(options),
36933695
},
@@ -4018,7 +4020,7 @@ class File extends ServiceObject<File, FileMetadata> {
40184020
async restore(options: RestoreOptions): Promise<File> {
40194021
const file = await this.storageTransport.makeRequest<File>({
40204022
method: 'POST',
4021-
url: `/b/${this.bucket.name}/o/${encodeURIComponent(this.name)}/restore`,
4023+
url: `/storage/v1/b/${this.bucket.name}/o/${encodeURIComponent(this.name)}/restore`,
40224024
queryParameters: options as unknown as StorageQueryParameters,
40234025
});
40244026
return file as File;

handwritten/storage/src/hmacKey.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ export class HmacKey extends ServiceObject<HmacKey, HmacKeyMetadata> {
354354
storageTransport: storage.storageTransport,
355355
parent: storage,
356356
id: accessId,
357-
baseUrl: `/projects/${projectId}/hmacKeys`,
357+
baseUrl: `/storage/v1/projects/${projectId}/hmacKeys`,
358358
methods,
359359
});
360360

handwritten/storage/src/iam.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ export enum IAMExceptionMessages {
141141
* ```
142142
*/
143143
class Iam {
144-
private resourceId_: string;
144+
private bucket: Bucket;
145145
private storageTransport: StorageTransport;
146146

147147
constructor(bucket: Bucket) {
148-
this.resourceId_ = 'buckets/' + bucket.getId();
148+
this.bucket = bucket;
149149
this.storageTransport = bucket.storageTransport;
150150
}
151151

@@ -261,7 +261,8 @@ class Iam {
261261
this.storageTransport
262262
.makeRequest(
263263
{
264-
url: '/iam',
264+
method: 'GET',
265+
url: `/storage/v1/b/${this.bucket.name}/iam`,
265266
queryParameters: qs as unknown as StorageQueryParameters,
266267
},
267268
(err, data, resp) => {
@@ -358,16 +359,10 @@ class Iam {
358359
.makeRequest(
359360
{
360361
method: 'PUT',
361-
url: '/iam',
362+
url: `/storage/v1/b/${this.bucket.name}/iam`,
362363
maxRetries,
363-
body: JSON.stringify(
364-
Object.assign(
365-
{
366-
resourceId: this.resourceId_,
367-
},
368-
policy,
369-
),
370-
),
364+
body: JSON.stringify(policy),
365+
headers: {'Content-Type': 'application/json'},
371366
queryParameters: options as unknown as StorageQueryParameters,
372367
},
373368
(err, data, resp) => {
@@ -467,17 +462,18 @@ class Iam {
467462
? permissions
468463
: [permissions];
469464

470-
const req = Object.assign(
471-
{
472-
permissions: permissionsArray,
473-
},
474-
options,
475-
);
465+
const req: any = {
466+
permissions: permissionsArray,
467+
};
468+
if (options.userProject) {
469+
req.userProject = options.userProject;
470+
}
476471

477472
this.storageTransport
478473
.makeRequest<TestPermissionsResponse>(
479474
{
480-
url: '/iam/testPermissions',
475+
method: 'GET',
476+
url: `/storage/v1/b/${this.bucket.name}/iam/testPermissions`,
481477
queryParameters: req as unknown as StorageQueryParameters,
482478
},
483479
(err, data, resp) => {

handwritten/storage/src/nodejs-common/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
export {GoogleAuthOptions} from 'google-auth-library';
1717

18-
export {ServiceConfig, ServiceOptions} from './service.js';
19-
2018
export {
2119
BaseMetadata,
2220
DeleteCallback,

handwritten/storage/src/nodejs-common/service.ts

Lines changed: 0 additions & 61 deletions
This file was deleted.

handwritten/storage/src/resumable-upload.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,8 @@ export class Upload extends Writable {
13551355
code: resp.status.toString(),
13561356
message: resp.statusText,
13571357
name: resp.statusText,
1358+
config: resp.config,
1359+
response: resp,
13581360
} as GaxiosError)
13591361
) {
13601362
void this.attemptDelayedRetry(resp);

0 commit comments

Comments
 (0)