in packages/storage/src/providers/AWSS3Provider.ts [245:350]
public async copy(
src: S3CopySource,
dest: S3CopyDestination,
config?: S3ProviderCopyConfig
): Promise<S3ProviderCopyOutput> {
const credentialsOK = await this._ensureCredentials();
if (!credentialsOK || !this._isWithCredentials(this._config)) {
throw new Error(StorageErrorStrings.NO_CREDENTIALS);
}
const opt = Object.assign({}, this._config, config);
const {
acl,
bucket,
cacheControl,
expires,
track,
serverSideEncryption,
SSECustomerAlgorithm,
SSECustomerKey,
SSECustomerKeyMD5,
SSEKMSKeyId,
} = opt;
const {
level: srcLevel = DEFAULT_STORAGE_LEVEL,
identityId: srcIdentityId,
key: srcKey,
} = src;
const { level: destLevel = DEFAULT_STORAGE_LEVEL, key: destKey } = dest;
if (!srcKey || typeof srcKey !== 'string') {
throw new Error(StorageErrorStrings.NO_SRC_KEY);
}
if (!destKey || typeof destKey !== 'string') {
throw new Error(StorageErrorStrings.NO_DEST_KEY);
}
if (srcLevel !== 'protected' && srcIdentityId) {
logger.warn(
`You may copy files from another user if the source level is "protected", currently it's ${srcLevel}`
);
}
const srcPrefix = this._prefix({
...opt,
level: srcLevel,
...(srcIdentityId && { identityId: srcIdentityId }),
});
const destPrefix = this._prefix({ ...opt, level: destLevel });
const finalSrcKey = `${bucket}/${srcPrefix}${srcKey}`;
const finalDestKey = `${destPrefix}${destKey}`;
logger.debug(`copying ${finalSrcKey} to ${finalDestKey}`);
const params: CopyObjectCommandInput = {
Bucket: bucket,
CopySource: finalSrcKey,
Key: finalDestKey,
// Copies over metadata like contentType as well
MetadataDirective: 'COPY',
};
if (cacheControl) params.CacheControl = cacheControl;
if (expires) params.Expires = expires;
if (serverSideEncryption) {
params.ServerSideEncryption = serverSideEncryption;
}
if (SSECustomerAlgorithm) {
params.SSECustomerAlgorithm = SSECustomerAlgorithm;
}
if (SSECustomerKey) {
params.SSECustomerKey = SSECustomerKey;
}
if (SSECustomerKeyMD5) {
params.SSECustomerKeyMD5 = SSECustomerKeyMD5;
}
if (SSEKMSKeyId) {
params.SSEKMSKeyId = SSEKMSKeyId;
}
if (acl) params.ACL = acl;
const s3 = this._createNewS3Client(opt);
try {
await s3.send(new CopyObjectCommand(params));
dispatchStorageEvent(
track,
'copy',
{
method: 'copy',
result: 'success',
},
null,
`Copy success from ${srcKey} to ${destKey}`
);
return {
key: destKey,
};
} catch (error) {
dispatchStorageEvent(
track,
'copy',
{
method: 'copy',
result: 'failed',
},
null,
`Copy failed from ${srcKey} to ${destKey}`
);
throw error;
}
}