func TestCDNObjects(t *testing.T) {
	raxClient, err := createClient(t, false)
	th.AssertNoErr(t, err)

	createContResult := raxContainers.Create(raxClient, "gophercloud-test", nil)
	th.AssertNoErr(t, createContResult.Err)
	t.Logf("Headers from Create Container request: %+v\n", createContResult.Header)
	defer func() {
		deleteResult := raxContainers.Delete(raxClient, "gophercloud-test")
		th.AssertNoErr(t, deleteResult.Err)
	}()

	header, err := raxObjects.Create(raxClient, "gophercloud-test", "test-object", bytes.NewBufferString("gophercloud cdn test"), nil).ExtractHeader()
	th.AssertNoErr(t, err)
	t.Logf("Headers from Create Object request: %+v\n", header)
	defer func() {
		deleteResult := raxObjects.Delete(raxClient, "gophercloud-test", "test-object", nil)
		th.AssertNoErr(t, deleteResult.Err)
	}()

	raxCDNClient, err := createClient(t, true)
	th.AssertNoErr(t, err)

	enableResult := raxCDNContainers.Enable(raxCDNClient, "gophercloud-test", raxCDNContainers.EnableOpts{CDNEnabled: true, TTL: 900})
	th.AssertNoErr(t, enableResult.Err)
	t.Logf("Headers from Enable CDN Container request: %+v\n", enableResult.Header)

	deleteResult := raxCDNObjects.Delete(raxCDNClient, "gophercloud-test", "test-object", nil)
	th.AssertNoErr(t, deleteResult.Err)
	t.Logf("Headers from Delete CDN Object request: %+v\n", deleteResult.Err)
}
Пример #2
0
func (command *commandUpload) Execute(resource *handler.Resource) {
	params := resource.Params.(*paramsUpload)
	containerName := params.container
	objectName := params.object
	opts := params.opts
	var readSeeker io.ReadSeeker
	var err error
	if stream := params.stream; stream {
		readSeeker = os.Stdin
	} else if fileName := params.fileName; fileName != "" {
		// this file will be closed by Gophercloud, if not closed before then
		readSeeker, err = os.Open(fileName)
		if err != nil {
			resource.Err = err
			return
		}
	} else {
		content := params.content
		readSeeker = strings.NewReader(content)
	}
	rawResponse := objects.Create(command.Ctx.ServiceClient, containerName, objectName, readSeeker, opts)
	if rawResponse.Err != nil {
		resource.Err = rawResponse.Err
		return
	}
	resource.Result = fmt.Sprintf("Successfully uploaded object [%s] to container [%s]\n", objectName, containerName)
}
Пример #3
0
// 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)
}
Пример #4
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)
}