1// Copyright... 2 3// This example demonstrates opening a Connection and doing some basic operations. 4package swift_test 5 6import ( 7 "context" 8 "fmt" 9 10 "github.com/ncw/swift/v2" 11) 12 13func ExampleConnection() { 14 ctx := context.Background() 15 // Create a v1 auth connection 16 c := &swift.Connection{ 17 // This should be your username 18 UserName: "user", 19 // This should be your api key 20 ApiKey: "key", 21 // This should be a v1 auth url, eg 22 // Rackspace US https://auth.api.rackspacecloud.com/v1.0 23 // Rackspace UK https://lon.auth.api.rackspacecloud.com/v1.0 24 // Memset Memstore UK https://auth.storage.memset.com/v1.0 25 AuthUrl: "auth_url", 26 } 27 28 // Authenticate 29 err := c.Authenticate(ctx) 30 if err != nil { 31 panic(err) 32 } 33 // List all the containers 34 containers, err := c.ContainerNames(ctx, nil) 35 if err != nil { 36 panic(err) 37 } 38 fmt.Println(containers) 39 // etc... 40 41 // ------ or alternatively create a v2 connection ------ 42 43 // Create a v2 auth connection 44 c = &swift.Connection{ 45 // This is the sub user for the storage - eg "admin" 46 UserName: "user", 47 // This should be your api key 48 ApiKey: "key", 49 // This should be a version2 auth url, eg 50 // Rackspace v2 https://identity.api.rackspacecloud.com/v2.0 51 // Memset Memstore v2 https://auth.storage.memset.com/v2.0 52 AuthUrl: "v2_auth_url", 53 // Region to use - default is use first region if unset 54 Region: "LON", 55 // Name of the tenant - this is likely your username 56 Tenant: "jim", 57 } 58 59 // as above... 60} 61 62var container string 63 64func ExampleConnection_ObjectsWalk() { 65 c, rollback := makeConnection(nil) 66 defer rollback() 67 68 objects := make([]string, 0) 69 err := c.ObjectsWalk(context.Background(), container, nil, func(ctx context.Context, opts *swift.ObjectsOpts) (interface{}, error) { 70 newObjects, err := c.ObjectNames(ctx, container, opts) 71 if err == nil { 72 objects = append(objects, newObjects...) 73 } 74 return newObjects, err 75 }) 76 fmt.Println("Found all the objects", objects, err) 77} 78 79func ExampleConnection_VersionContainerCreate() { 80 c, rollback := makeConnection(nil) 81 defer rollback() 82 83 // Use the helper method to create the current and versions container. 84 if err := c.VersionContainerCreate(context.Background(), "cds", "cd-versions"); err != nil { 85 fmt.Print(err.Error()) 86 } 87} 88 89func ExampleConnection_VersionEnable() { 90 ctx := context.Background() 91 c, rollback := makeConnection(nil) 92 defer rollback() 93 94 // Build the containers manually and enable them. 95 if err := c.ContainerCreate(ctx, "movie-versions", nil); err != nil { 96 fmt.Print(err.Error()) 97 } 98 if err := c.ContainerCreate(ctx, "movies", nil); err != nil { 99 fmt.Print(err.Error()) 100 } 101 if err := c.VersionEnable(ctx, "movies", "movie-versions"); err != nil { 102 fmt.Print(err.Error()) 103 } 104 105 // Access the primary container as usual with ObjectCreate(), ObjectPut(), etc. 106 // etc... 107} 108 109func ExampleConnection_VersionDisable() { 110 c, rollback := makeConnection(nil) 111 defer rollback() 112 113 // Disable versioning on a container. Note that this does not delete the versioning container. 114 err := c.VersionDisable(context.Background(), "movies") 115 if err != nil { 116 panic(err) 117 } 118} 119