Skip to content

Commit

Permalink
First cut of Swift v2
Browse files Browse the repository at this point in the history
  • Loading branch information
markfeit committed Dec 4, 2021
1 parent e43e848 commit c363d21
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/duplicacy_swiftstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type SwiftStorage struct {
StorageBase

ctx *context.Context
connection *swift.Connection
container string
storageDir string
Expand Down Expand Up @@ -106,6 +107,8 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw
arguments["protocol"] = "https"
}

ctx := context.Context()

// Please refer to https://godoc.org/github.com/ncw/swift#Connection
connection := swift.Connection{
Domain: arguments["domain"],
Expand All @@ -129,17 +132,18 @@ func CreateSwiftStorage(storageURL string, key string, threads int) (storage *Sw
TrustId: arguments["trust_id"],
}

err = connection.Authenticate()
err = connection.Authenticate(ctx)
if err != nil {
return nil, err
}

_, _, err = connection.Container(container)
_, _, err = connection.Container(ctx, container)
if err != nil {
return nil, err
}

storage = &SwiftStorage{
context: &ctx,
connection: &connection,
container: container,
storageDir: storageDir,
Expand Down Expand Up @@ -168,7 +172,7 @@ func (storage *SwiftStorage) ListFiles(threadIndex int, dir string) (files []str
options.Delimiter = '/'
}

objects, err := storage.connection.ObjectsAll(storage.container, &options)
objects, err := storage.connection.ObjectsAll(storage.ctx, storage.container, &options)
if err != nil {
return nil, nil, err
}
Expand All @@ -190,12 +194,12 @@ func (storage *SwiftStorage) ListFiles(threadIndex int, dir string) (files []str

// DeleteFile deletes the file or directory at 'filePath'.
func (storage *SwiftStorage) DeleteFile(threadIndex int, filePath string) (err error) {
return storage.connection.ObjectDelete(storage.container, storage.storageDir+filePath)
return storage.connection.ObjectDelete(storage.ctx, storage.container, storage.storageDir+filePath)
}

// MoveFile renames the file.
func (storage *SwiftStorage) MoveFile(threadIndex int, from string, to string) (err error) {
return storage.connection.ObjectMove(storage.container, storage.storageDir+from,
return storage.connection.ObjectMove(storage.ctx, storage.container, storage.storageDir+from,
storage.container, storage.storageDir+to)
}

Expand All @@ -207,7 +211,7 @@ func (storage *SwiftStorage) CreateDirectory(threadIndex int, dir string) (err e

// GetFileInfo returns the information about the file or directory at 'filePath'.
func (storage *SwiftStorage) GetFileInfo(threadIndex int, filePath string) (exist bool, isDir bool, size int64, err error) {
object, _, err := storage.connection.Object(storage.container, storage.storageDir+filePath)
object, _, err := storage.connection.Object(storage.ctx, storage.container, storage.storageDir+filePath)

if err != nil {
if err == swift.ObjectNotFound {
Expand All @@ -223,7 +227,7 @@ func (storage *SwiftStorage) GetFileInfo(threadIndex int, filePath string) (exis
// DownloadFile reads the file at 'filePath' into the chunk.
func (storage *SwiftStorage) DownloadFile(threadIndex int, filePath string, chunk *Chunk) (err error) {

file, _, err := storage.connection.ObjectOpen(storage.container, storage.storageDir+filePath, false, nil)
file, _, err := storage.connection.ObjectOpen(storage.ctx, storage.container, storage.storageDir+filePath, false, nil)
if err != nil {
return err
}
Expand All @@ -234,7 +238,7 @@ func (storage *SwiftStorage) DownloadFile(threadIndex int, filePath string, chun
// UploadFile writes 'content' to the file at 'filePath'.
func (storage *SwiftStorage) UploadFile(threadIndex int, filePath string, content []byte) (err error) {
reader := CreateRateLimitedReader(content, storage.UploadRateLimit/storage.threads)
_, err = storage.connection.ObjectPut(storage.container, storage.storageDir+filePath, reader, true, "", "application/duplicacy", nil)
_, err = storage.connection.ObjectPut(storage.ctx, storage.container, storage.storageDir+filePath, reader, true, "", "application/duplicacy", nil)
return err
}

Expand Down

0 comments on commit c363d21

Please sign in to comment.