File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -67,6 +67,12 @@ client.deletePaths(paths);
67
67
68
68
- ** paths** : Array of relative paths in DO spaces to delete
69
69
70
+ ### Copy file within DO Spaces
71
+
72
+ ``` js
73
+ client .copyFile (sourcePath, destinationPath);
74
+ ```
75
+
70
76
### Download file
71
77
72
78
``` js
Original file line number Diff line number Diff line change @@ -47,6 +47,20 @@ class SpacesClient {
47
47
return url . replace ( / d i g i t a l o c e a n s p a c e s / , 'cdn.digitaloceanspaces' ) ;
48
48
}
49
49
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 ( / A l l U s e r s / ) ;
58
+ const isRead = Permission === 'READ' ;
59
+ return isAllUsers && isRead ;
60
+ } ) ;
61
+ return isPublic ;
62
+ }
63
+
50
64
async uploadFile (
51
65
uploadFilePath ,
52
66
destinationPath ,
@@ -121,6 +135,20 @@ class SpacesClient {
121
135
return this . deleteObjects ( objects ) ;
122
136
}
123
137
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
+
124
152
async downloadFile (
125
153
filePathToRead ,
126
154
filePathToSave ,
You can’t perform that action at this time.
0 commit comments