Skip to content

Commit 8889fa4

Browse files
committed
Add "copyFile" function, to copy files within the same bucket
1 parent 82a9847 commit 8889fa4

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ client.deletePaths(paths);
6767

6868
- **paths** : Array of relative paths in DO spaces to delete
6969

70+
### Copy file within DO Spaces
71+
72+
```js
73+
client.copyFile(sourcePath, destinationPath);
74+
```
75+
7076
### Download file
7177

7278
```js

src/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ class SpacesClient {
4747
return url.replace(/digitaloceanspaces/, 'cdn.digitaloceanspaces');
4848
}
4949

50+
async isFilePublic(filePath) {
51+
const acl = await this.s3client.getObjectAcl({
52+
Bucket: this.bucket,
53+
Key: filePath,
54+
}).promise();
55+
56+
const isPublic = acl.Grants.some(({ Grantee, Permission }) => {
57+
const isAllUsers = Grantee.URI && Grantee.URI.match(/AllUsers/);
58+
const isRead = Permission === 'READ';
59+
return isAllUsers && isRead;
60+
});
61+
return isPublic;
62+
}
63+
5064
async uploadFile(
5165
uploadFilePath,
5266
destinationPath,
@@ -121,6 +135,20 @@ class SpacesClient {
121135
return this.deleteObjects(objects);
122136
}
123137

138+
async copyFile(
139+
sourcePath,
140+
destinationPath,
141+
) {
142+
const isSourcePublic = await this.isFilePublic(sourcePath);
143+
144+
return this.s3client.copyObject({
145+
Bucket: this.bucket,
146+
CopySource: `/${this.bucket}/${sourcePath}`,
147+
Key: destinationPath,
148+
ACL: isSourcePublic ? 'public-read' : 'private',
149+
}).promise();
150+
}
151+
124152
async downloadFile(
125153
filePathToRead,
126154
filePathToSave,

0 commit comments

Comments
 (0)