Skip to content

Commit

Permalink
Fix s3 path separator on Windows (#210)
Browse files Browse the repository at this point in the history
Co-authored-by: Ronaldo Ropelatto <[email protected]>
  • Loading branch information
Ropelatto and Ronaldo Ropelatto authored Jan 11, 2022
1 parent b3609f2 commit 7bfff33
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
6 changes: 4 additions & 2 deletions s3path.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package s3sync
import (
"errors"
"net/url"
"path/filepath"
"strings"
)

Expand All @@ -32,8 +33,9 @@ func urlToS3Path(url *url.URL) (*s3Path, error) {
}

return &s3Path{
bucket: url.Host,
bucketPrefix: strings.TrimPrefix(url.Path, "/"),
bucket: url.Host,
// Using filepath.ToSlash for change backslash to slash on Windows
bucketPrefix: strings.TrimPrefix(filepath.ToSlash(url.Path), "/"),
}, nil
}

Expand Down
9 changes: 6 additions & 3 deletions s3sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ func (m *Manager) download(file *fileInfo, sourcePath *s3Path, destPath string)
if file.singleFile {
sourceFile = file.name
} else {
sourceFile = filepath.Join(sourcePath.bucketPrefix, file.name)
// Using filepath.ToSlash for change backslash to slash on Windows
sourceFile = filepath.ToSlash(filepath.Join(sourcePath.bucketPrefix, file.name))
}

_, err = s3manager.NewDownloaderWithClient(
Expand Down Expand Up @@ -282,7 +283,8 @@ func (m *Manager) upload(file *fileInfo, sourcePath string, destPath *s3Path) er
destFile := *destPath
if strings.HasSuffix(destPath.bucketPrefix, "/") || destPath.bucketPrefix == "" || !file.singleFile {
// If source is a single file and destination is not a directory, use destination URL as is.
destFile.bucketPrefix = filepath.Join(destPath.bucketPrefix, file.name)
// Using filepath.ToSlash for change backslash to slash on Windows
destFile.bucketPrefix = filepath.ToSlash(filepath.Join(destPath.bucketPrefix, file.name))
}

println("Uploading", file.name, "to", destFile.String())
Expand Down Expand Up @@ -332,7 +334,8 @@ func (m *Manager) deleteRemote(file *fileInfo, destPath *s3Path) error {
destFile := *destPath
if strings.HasSuffix(destPath.bucketPrefix, "/") || destPath.bucketPrefix == "" || !file.singleFile {
// If source is a single file and destination is not a directory, use destination URL as is.
destFile.bucketPrefix = filepath.Join(destPath.bucketPrefix, file.name)
// Using filepath.ToSlash for change backslash to slash on Windows
destFile.bucketPrefix = filepath.ToSlash(filepath.Join(destPath.bucketPrefix, file.name))
}

println("Deleting", destFile.String())
Expand Down

0 comments on commit 7bfff33

Please sign in to comment.