Last active
July 18, 2017 10:40
-
-
Save nono/e451e35ef12417eb7d239611281f8edc to your computer and use it in GitHub Desktop.
Debug renaming big files in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/bug-rename-swift | |
/random | |
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"math/rand" | |
"net/url" | |
"os" | |
"time" | |
"github.com/cozy/swift" | |
) | |
const ( | |
host = "swift-dev:35357" | |
username = "admin" | |
password = "secret" | |
container = "cozy-bug-rename-big-file-swift" | |
version = container + "-version" | |
filename = "random" | |
) | |
func randomString(n int) string { | |
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
b := make([]byte, n) | |
lenLetters := len(letters) | |
for i := 0; i < n; i++ { | |
b[i] = letters[rand.Intn(lenLetters)] | |
} | |
return string(b) | |
} | |
func main() { | |
authURL := &url.URL{ | |
Scheme: "http", | |
Host: host, | |
Path: "/identity/v3", | |
} | |
conn := &swift.Connection{ | |
UserName: username, | |
ApiKey: password, | |
AuthUrl: authURL.String(), | |
Domain: "default", | |
Tenant: "admin", | |
TenantId: "", | |
TenantDomain: "", | |
TenantDomainId: "", | |
} | |
// Authenticate | |
if err := conn.Authenticate(); err != nil { | |
fmt.Printf("Authentication failed with the Swift server on %s\n", authURL) | |
return | |
} | |
fmt.Printf("Successfully authenticated with server %s\n", authURL) | |
// Initialize container | |
if err := conn.VersionContainerCreate(container, version); err != nil { | |
fmt.Printf("Could not create container %s: %s\n", container, err.Error()) | |
return | |
} | |
fmt.Printf("Created container %s\n", container) | |
// Upload the file | |
f, err := os.Open(filename) | |
if err != nil { | |
fmt.Printf("Cannot open file %s: %s\n", filename, err) | |
return | |
} | |
defer f.Close() | |
start := time.Now() | |
name := "random-" + randomString(8) | |
mime := "application/octet-stream" | |
w, err := conn.ObjectCreate(container, name, false, "", mime, nil) | |
if err != nil { | |
fmt.Printf("Cannot create object %s: %s\n", name, err) | |
return | |
} | |
io.Copy(w, f) | |
if err = w.Close(); err != nil { | |
fmt.Printf("Error while uploading %s: %s\n", name, err) | |
return | |
} | |
elapsed := time.Since(start) | |
fmt.Printf("Upload success: %s (%s)\n", name, elapsed) | |
// Rename the file -> copy + delete | |
start = time.Now() | |
dst := name + "-renamed" | |
_, err = conn.ObjectCopy(container, name, container, dst, nil) | |
elapsed = time.Since(start) | |
if err != nil { | |
elapsed = time.Since(start) | |
fmt.Printf("Error while copying %s: %s (%s)\n", name, err, elapsed) | |
return | |
} | |
fmt.Printf("Copy success %s -> %s (%s)\n", name, dst, elapsed) | |
start = time.Now() | |
if err := conn.ObjectDelete(container, name); err != nil { | |
fmt.Printf("Error while deleting %s: %s\n", name, err) | |
return | |
} | |
elapsed = time.Since(start) | |
fmt.Printf("Delete success %s (%s)\n", name, elapsed) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment