@@ -199,14 +199,34 @@ func (c Controller) moveDown(prefix string) {
199
199
func (c Controller ) Download (key string ) {
200
200
c .Debugf ("bucket=%s prefix=%s key=%s\n " , c .m .Bucket (), c .m .Prefix (), key )
201
201
202
+ cwd , err := os .Getwd ()
203
+ if err != nil {
204
+ panic (err )
205
+ }
206
+ cwd = cwd + "/"
207
+
202
208
totalSize := int64 (0 )
203
209
existFilePath := []string {}
204
210
objects := c .m .ListObjects (key )
211
+ destPathMap := map [string ]string {}
205
212
for _ , object := range objects {
206
- filePath := aws .ToString (object .Key )
207
- c .Debugf ("- %s\n " , filePath )
208
- if _ , err := os .Stat (filePath ); err == nil {
209
- existFilePath = append (existFilePath , filePath )
213
+ key := aws .ToString (object .Key )
214
+ // download into under current directory
215
+ destPath , err := filepath .Abs ("./" + key )
216
+ if err != nil {
217
+ panic (err )
218
+ }
219
+
220
+ // just to be safe
221
+ if ! strings .HasPrefix (destPath , cwd ) {
222
+ panic (fmt .Sprintf ("destPath is not under current directory: destPath=%s cwd=%s" , destPath , cwd ))
223
+ }
224
+
225
+ destPathMap [key ] = destPath
226
+
227
+ c .Debugf ("- %s %s\n " , key , destPath )
228
+ if _ , err := os .Stat (destPath ); err == nil {
229
+ existFilePath = append (existFilePath , destPath )
210
230
}
211
231
totalSize += object .Size
212
232
}
@@ -217,10 +237,6 @@ func (c Controller) Download(key string) {
217
237
}
218
238
219
239
// check disk available
220
- cwd , err := os .Getwd ()
221
- if err != nil {
222
- panic (err )
223
- }
224
240
usage , err := disk .Usage (cwd )
225
241
if err != nil {
226
242
panic (err )
@@ -253,8 +269,9 @@ func (c Controller) Download(key string) {
253
269
title := "Downloading"
254
270
255
271
for i , object := range objects {
256
- c .Debugf ("download %s\n " , aws .ToString (object .Key ))
257
- n , err := c .m .Download (object )
272
+ key := aws .ToString (object .Key )
273
+ c .Debugf ("download %s\n " , key )
274
+ n , err := c .m .Download (object , destPathMap [key ])
258
275
259
276
if err != nil {
260
277
panic (err )
0 commit comments