Exemplo n.º 1
0
// 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)
	}
}
Exemplo n.º 2
0
// 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)
	}
}
Exemplo n.º 3
0
func TestObjects(t *testing.T) {
	c, err := createClient(t, false)
	th.AssertNoErr(t, err)

	res := raxContainers.Create(c, "gophercloud-test", nil)
	th.AssertNoErr(t, res.Err)

	defer func() {
		t.Logf("Deleting container...")
		res := raxContainers.Delete(c, "gophercloud-test")
		th.AssertNoErr(t, res.Err)
	}()

	content := bytes.NewBufferString("Lewis Carroll")
	options := &osObjects.CreateOpts{ContentType: "text/plain"}
	createres := raxObjects.Create(c, "gophercloud-test", "o1", content, options)
	th.AssertNoErr(t, createres.Err)

	defer func() {
		t.Logf("Deleting object o1...")
		res := raxObjects.Delete(c, "gophercloud-test", "o1", nil)
		th.AssertNoErr(t, res.Err)
	}()

	t.Logf("Objects Info available to the currently issued token:")
	count := 0
	err = raxObjects.List(c, "gophercloud-test", &osObjects.ListOpts{Full: true}).EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page %02d ---", count)

		objects, err := raxObjects.ExtractInfo(page)
		th.AssertNoErr(t, err)

		for i, object := range objects {
			t.Logf("[%02d]      name=[%s]", i, object.Name)
			t.Logf("            content-type=[%s]", object.ContentType)
			t.Logf("            bytes=[%d]", object.Bytes)
			t.Logf("            last-modified=[%s]", object.LastModified)
			t.Logf("            hash=[%s]", object.Hash)
		}

		count++
		return true, nil
	})
	th.AssertNoErr(t, err)
	if count == 0 {
		t.Errorf("No objects listed for your current token.")
	}
	t.Logf("Container Names available to the currently issued token:")
	count = 0
	err = raxObjects.List(c, "gophercloud-test", &osObjects.ListOpts{Full: false}).EachPage(func(page pagination.Page) (bool, error) {
		t.Logf("--- Page %02d ---", count)

		names, err := raxObjects.ExtractNames(page)
		th.AssertNoErr(t, err)

		for i, name := range names {
			t.Logf("[%02d] %s", i, name)
		}

		count++
		return true, nil
	})
	th.AssertNoErr(t, err)
	if count == 0 {
		t.Errorf("No objects listed for your current token.")
	}

	copyres := raxObjects.Copy(c, "gophercloud-test", "o1", &raxObjects.CopyOpts{Destination: "gophercloud-test/o2"})
	th.AssertNoErr(t, copyres.Err)
	defer func() {
		t.Logf("Deleting object o2...")
		res := raxObjects.Delete(c, "gophercloud-test", "o2", nil)
		th.AssertNoErr(t, res.Err)
	}()

	o1Content, err := raxObjects.Download(c, "gophercloud-test", "o1", nil).ExtractContent()
	th.AssertNoErr(t, err)
	o2Content, err := raxObjects.Download(c, "gophercloud-test", "o2", nil).ExtractContent()
	th.AssertNoErr(t, err)
	th.AssertEquals(t, string(o2Content), string(o1Content))

	updateres := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": "mountains"}})
	th.AssertNoErr(t, updateres.Err)
	t.Logf("Headers from Update Account request: %+v\n", updateres.Header)
	defer func() {
		res := raxObjects.Update(c, "gophercloud-test", "o2", osObjects.UpdateOpts{Metadata: map[string]string{"white": ""}})
		th.AssertNoErr(t, res.Err)
		metadata, err := raxObjects.Get(c, "gophercloud-test", "o2", nil).ExtractMetadata()
		th.AssertNoErr(t, err)
		t.Logf("Metadata from Get Account request (after update reverted): %+v\n", metadata)
		th.CheckEquals(t, "", metadata["White"])
	}()

	getres := raxObjects.Get(c, "gophercloud-test", "o2", nil)
	th.AssertNoErr(t, getres.Err)
	t.Logf("Headers from Get Account request (after update): %+v\n", getres.Header)
	metadata, err := getres.ExtractMetadata()
	th.AssertNoErr(t, err)
	t.Logf("Metadata from Get Account request (after update): %+v\n", metadata)
	th.CheckEquals(t, "mountains", metadata["White"])

	createTempURLOpts := osObjects.CreateTempURLOpts{
		Method: osObjects.GET,
		TTL:    600,
	}
	tempURL, err := raxObjects.CreateTempURL(c, "gophercloud-test", "o1", createTempURLOpts)
	th.AssertNoErr(t, err)
	t.Logf("TempURL for object (%s): %s", "o1", tempURL)
}