Skip to content

Commit

Permalink
feat: allow removal of resumable upload cache (#773)
Browse files Browse the repository at this point in the history
* Upgraded gcs-resumable-upload to 2.2.2 #217

- Added resumable cache cleanup
  • Loading branch information
jiren authored and stephenplusplus committed Sep 18, 2019
1 parent e9ec9cf commit da943db
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"duplexify": "^3.5.0",
"extend": "^3.0.2",
"gaxios": "^2.0.1",
"gcs-resumable-upload": "^2.0.0",
"gcs-resumable-upload": "^2.2.4",
"hash-stream-validation": "^0.2.1",
"mime": "^2.2.0",
"mime-types": "^2.0.8",
Expand Down
41 changes: 41 additions & 0 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,47 @@ class File extends ServiceObject<File> {
return stream as Writable;
}

/**
* Delete failed resumable upload file cache.
*
* Resumable file upload cache the config file to restart upload in case of
* failure. In certain scenarios, the resumable upload will not works and
* upload file cache needs to be deleted to upload the same file.
*
* Following are some of the scenarios.
*
* Resumable file upload failed even though the file is successfully saved
* on the google storage and need to clean up a resumable file cache to
* update the same file.
*
* Resumable file upload failed due to pre-condition
* (i.e generation number is not matched) and want to upload a same
* file with the new generation number.
*
* @example
* const {Storage} = require('@google-cloud/storage');
* const storage = new Storage();
* const myBucket = storage.bucket('my-bucket');
*
* const file = myBucket.file('my-file', { generation: 0 });
* const contents = 'This is the contents of the file.';
*
* file.save(contents, function(err) {
* if (err) {
* file.deleteResumableCache();
* }
* });
*
*/
deleteResumableCache() {
const uploadStream = resumableUpload.upload({
bucket: this.bucket.name,
file: this.name,
generation: this.generation,
});
uploadStream.deleteConfig();
}

download(options?: DownloadOptions): Promise<DownloadResponse>;
download(options: DownloadOptions, callback: DownloadCallback): void;
download(callback: DownloadCallback): void;
Expand Down
23 changes: 23 additions & 0 deletions system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3003,6 +3003,29 @@ describe('storage', () => {
files![1].metadata.generation
);
});

it('should throw an error Precondition Failed on overwrite with version 0, then save file with and without resumable', async () => {
const fileName = `test-${Date.now()}.txt`;

await bucketWithVersioning
.file(fileName)
.save('hello1', {resumable: false});
await assert.rejects(
async () => {
await bucketWithVersioning
.file(fileName, {generation: 0})
.save('hello2');
},
{
code: 412,
message: 'Precondition Failed',
}
);
await bucketWithVersioning
.file(fileName)
.save('hello3', {resumable: false});
await bucketWithVersioning.file(fileName).save('hello4');
});
});

describe('v2 signed urls', () => {
Expand Down
22 changes: 22 additions & 0 deletions test/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,28 @@ describe('File', () => {
});
});

describe('deleteResumableCache', () => {
it('should delete resumable file upload cache', done => {
file.generation = 123;

resumableUploadOverride = {
// tslint:disable-next-line no-any
upload(opts: any) {
assert.strictEqual(opts.bucket, file.bucket.name);
assert.strictEqual(opts.file, file.name);
assert.strictEqual(opts.generation, file.generation);

return {
deleteConfig: () => {
done();
},
};
},
};
file.deleteResumableCache();
});
});

describe('download', () => {
let fileReadStream: Readable;

Expand Down

0 comments on commit da943db

Please sign in to comment.