// delete cloud files object from container func Delete(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") containerName := c.String("container") objectName := c.String("name") // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err := rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { fmt.Println(err) } _, err2 := objects.Delete(serviceClient, containerName, objectName, nil).Extract() if err2 != nil { fmt.Println(err2) } fmt.Printf("%v Deleted.", objectName) }
// create a cloud files container func Create(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") containerName := c.Args().First() if c.String("name") != "" { containerName = c.String("name") } // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err := rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { fmt.Println(err) } err2 := containers.Create(serviceClient, containerName, nil) fmt.Println(err2) }
// delete all cloud files object from container func DeleteAll(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") containerName := c.String("container") useConcurrency := c.String("concurrency") // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err := rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { fmt.Println(err) } // get objects List and delete each err3 := objects.List(serviceClient, containerName, nil).EachPage(func(page pagination.Page) (bool, error) { objectList, err4 := objects.ExtractNames(page) // https://github.com/rackspace/gophercloud/blob/master/openstack/blockstorage/v1/volumes/results.go for _, objectName := range objectList { // now delete each object here // fmt.Println(objName) if strings.Compare(useConcurrency, "TRUE") == 0 { go deleteObj(serviceClient, containerName, objectName) } else { deleteObj(serviceClient, containerName, objectName) } } if err4 != nil { fmt.Println(err4) } return true, nil }) if err3 != nil { fmt.Println(err3) } }
// upload file to Cloud File container func Upload(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") containerName := c.String("container") objectName := c.String("name") path := c.String("path") // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err := rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { fmt.Println(err) } f, err2 := os.Open(path) if err2 != nil { fmt.Println(err2) } defer f.Close() // reader := bufio.NewReader(f) _, err3 := objects.Create( serviceClient, containerName, objectName, f, nil, ).ExtractHeader() if err3 != nil { fmt.Println(err3) } fmt.Printf("%v Uploaded.", path) }
// print list of cloud files containers to stdout func List(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") containerName := c.Args().First() if c.String("container") != "" { containerName = c.String("container") } // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err := rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { fmt.Println(err) } // _, err := containers.Get(serviceClient, "{containerName}").ExtractMetadata() err3 := objects.List(serviceClient, containerName, nil).EachPage(func(page pagination.Page) (bool, error) { objectList, err4 := objects.ExtractNames(page) // https://github.com/rackspace/gophercloud/blob/master/openstack/blockstorage/v1/volumes/results.go for _, o := range objectList { fmt.Println(o) } if err4 != nil { fmt.Println(err4) } return true, nil }) if err3 != nil { fmt.Println(err3) } }
// print details of cloud files object to stdout func Show(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") containerName := c.String("container") objectName := c.String("name") // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err := rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { fmt.Println(err) } myObject, err3 := objects.Get(serviceClient, containerName, objectName, nil).Extract() if err3 != nil { fmt.Println(err3) } fmt.Println("Name: ", objectName) fmt.Println("Bytes: ", myObject.ContentLength) fmt.Println("Type: ", myObject.ContentType) fmt.Println("Last Modified: ", myObject.LastModified) }
func authFromScratch(ao gophercloud.AuthOptions, region, serviceType string) (*gophercloud.ServiceClient, error) { pc, err := rackspace.AuthenticatedClient(ao) if err != nil { return nil, err } var sc *gophercloud.ServiceClient switch serviceType { case "compute": sc, err = rackspace.NewComputeV2(pc, gophercloud.EndpointOpts{ Region: region, }) break case "object-store": sc, err = rackspace.NewObjectStorageV1(pc, gophercloud.EndpointOpts{ Region: region, }) break case "blockstorage": sc, err = rackspace.NewBlockStorageV1(pc, gophercloud.EndpointOpts{ Region: region, }) break case "network": sc, err = rackspace.NewNetworkV2(pc, gophercloud.EndpointOpts{ Region: region, }) break } if err != nil { return nil, err } if sc == nil { return nil, fmt.Errorf("Unable to create service client: Unknown service type: %s", serviceType) } sc.UserAgent.Prepend(util.UserAgent) return sc, nil }
// download file from Cloud File container func Download(c *cli.Context) { // assign vars from cli args user := c.String("user") key := c.String("key") region := c.String("region") containerName := c.String("container") objectName := c.String("name") path := c.String("path") // step 1, set up auth options ao := gophercloud.AuthOptions{ Username: user, APIKey: key, } // step 2, rax auth to get back provider instance provider, err := rackspace.AuthenticatedClient(ao) if err != nil { fmt.Println(err) } // set rax region serviceClient, err := rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) if err != nil { fmt.Println(err) } result := objects.Download(serviceClient, containerName, objectName, nil) content, err := result.ExtractContent() err3 := ioutil.WriteFile(path+"/"+objectName, []byte(content), 0644) if err3 != nil { fmt.Println(err3) } fmt.Printf("%v Downloaded.", objectName) }
func createClient(t *testing.T, cdn bool) (*gophercloud.ServiceClient, error) { region := os.Getenv("RS_REGION") if region == "" { t.Fatal("Please provide a Rackspace region as RS_REGION") } ao := rackspaceAuthOptions(t) provider, err := rackspace.NewClient(ao.IdentityEndpoint) th.AssertNoErr(t, err) err = rackspace.Authenticate(provider, ao) th.AssertNoErr(t, err) if cdn { return rackspace.NewObjectCDNV1(provider, gophercloud.EndpointOpts{ Region: region, }) } return rackspace.NewObjectStorageV1(provider, gophercloud.EndpointOpts{ Region: region, }) }