Skip to content

Commit

Permalink
chore(upload): Upload is optimized to use copyObject instead of putOb…
Browse files Browse the repository at this point in the history
…ject when content didn't changed
  • Loading branch information
yvele committed Jul 4, 2016
1 parent 624a5f7 commit 7fc331f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/poosh-plugin-s3/src/RemoteClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ export default class RemoteClient {
* @param file
*/
async upload (file: Object) {

if (file.dest.statusDetails.content === RemoteStatus.Same) {
// If content has not changed, we can simply make make a small "self copy"
// that will be faster
let params = this._paramsProvider.getSelfCopyObjectParams(file);
await this._s3.copyObjectAsync(params);
return;
}

let params = this._paramsProvider.getPutObjectParams(file);
await this._s3.putObjectAsync(params);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/poosh-plugin-s3/src/S3ParamsProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import pick from "lodash/pick";
import forOwn from "lodash/forOwn";
import joinUrl from "poosh-common/lib/url/join";
import parseMs from "poosh-common/lib/options/parseMs";
import RemoteStatus from "poosh-common/lib/file/RemoteStatus";
import { HEADERS_TO_PARAMS_MAP } from "./helpers/convertion";
import { FILE_REMOTE_TO_PARAMS_MAP } from "./helpers/convertion";

Expand Down Expand Up @@ -156,6 +157,31 @@ export default class S3ParamsProvider {
};
}

/**
* Build S3 copyObject params to make a self copy.
*
* @param file
* @returns Params.
*/
getSelfCopyObjectParams (file: Object): Object {
let params = this.getParamsWithKey(file);

// Self copy
params.CopySource = this._options.bucket + "/" + params.Key;

// Only append header params if something has changed
if (file.dest.statusDetails.headers === RemoteStatus.Different) {
appendParamsFromHeaders(params, file.headers);
}

// Only append file-remote params if something has changed
if (file.dest.statusDetails.remote === RemoteStatus.Different) {
appendParamsFromFileRemote(params, file.remote);
}

return params;
}

/**
* Build S3single item request params.
*
Expand Down
8 changes: 7 additions & 1 deletion packages/poosh-plugin-s3/src/promisification/S3.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Promise from "bluebird";
import { S3 } from "aws-sdk";

const METHODS = ["putObject", "headObject", "deleteObjects", "listObjects"];
const METHODS = [
"putObject",
"headObject",
"deleteObjects",
"listObjects",
"copyObject"
];

Promise.promisifyAll(
Reflect.getPrototypeOf(new S3()),
Expand Down

0 comments on commit 7fc331f

Please sign in to comment.