Object Operations

Last updated: 2021-06-11 11:09:58

    Overview

    This document provides an overview of APIs and SDK code samples related to simple operations, multipart operations, and advanced APIs.

    Simple operations

    API Operation Description
    GET Bucket (List Objects) Querying an object list Queries some or all objects in a bucket
    PUT Object Uploading an object using simple upload Uploads an object to a bucket
    HEAD Object Gets object metadata Gets the meta information of an object
    GET Object Downloading an object Downloads an object (file) to the local file system
    PUT Object - Copy Copying an object Copies an object to a destination path
    DELETE Object Deleting a single object Deletes a specified object from a bucket
    DELETE Multiple Object Deleting multiple objects Deletes multiple objects from a bucket
    POST Object restore Restoring an archived object Restores an archived object for access

    Multipart upload operations

    API Operation Description
    List Multipart Uploads Querying multipart uploads Queries in-progress multipart upload operations
    Initiate Multipart Upload Initializing a multipart upload operation Initializes a multipart upload operation
    Upload Part Uploading a part Uploads a part in a multipart upload
    List Parts Querying uploaded parts Queries the uploaded parts of a multipart upload
    Complete Multipart Upload Completing a multipart upload Completes the multipart upload of an entire file
    Abort Multipart Upload Aborting a multipart upload operation Aborts a multipart upload and deletes the uploaded parts

    Simple Operations

    Querying an object list

    API description

    This API is used to query some or all objects in a bucket.

    Method prototype

    go
    func (s *BucketService) Get(ctx context.Context, opt *BucketGetOptions) (*BucketGetResult, *Response, error)
    

    Sample request

    go
    opt := &cos.BucketGetOptions{
       Prefix:  "test",
       MaxKeys: 100,
    }
    _, _, err := client.Bucket.Get(context.Background(), opt)
    if err != nil {
       panic(err)
    }
    

    Parameter description

    go
    type BucketGetOptions struct {
    Prefix       string 
    Delimiter    string 
    EncodingType string 
    Marker       string 
    MaxKeys      int    
    }
    
    Parameter Description Type Required
    Prefix Filters object keys prefixed with the value of this parameter. It is left empty by default. string No
    Delimiter A separator that is left empty by default. For example, you can specify it as / to indicate folders. string No
    EncodingType Specifies the encoding method of the returned value. It is left empty by default. Valid value: url. string No
    Marker Specifies the object after which the listing should begin. Objects are listed using UTF-8 binary order by default. string No
    MaxKeys Maximum number of returned objects. It defaults to 1000. int No

    Response description

    go
    type BucketGetResult struct {
    Name           string
    Prefix         string 
    Marker         string 
    NextMarker     string 
    Delimiter      string 
    MaxKeys        int
    IsTruncated    bool
    Contents       []Object 
    CommonPrefixes []string 
    EncodingType   string   
    }
    
    Parameter Description Type
    Name Bucket name in the format: BucketName-APPID, e.g. examplebucket-1250000000. string
    Prefix Filters the object keys prefixed with the value of this parameter. It is left empty by default. string
    Marker Specifies the object after which the listing should begin. Objects are listed using UTF-8 binary order by default. string
    NextMarker Specifies the object after which the next listing should begin if IsTruncated is true. string
    Delimiter A separator that is left empty by default. For example, you can specify it as / to indicate folders. string
    MaxKeys Maximum number of returned objects. It defaults to 1000. int
    IsTruncated Indicates whether the returned objects are truncated bool
    Contents Lists metadata of all objects, including ETag, StorageClass, Key, Owner, LastModified, and Size. []Object
    CommonPrefixes All keys starting with Prefix and ending with Delimiter are grouped as a common prefix. []string
    EncodingType Specifies the encoding method of the returned value. It is left empty by default. Valid value: url. string

    Uploading an object using simple upload

    API description

    This API (PUT Object) is used to upload an object (file) of up to 5 GB to a bucket. For objects larger than 5 GB, please use multipart upload or advanced APIs. Simple uploads, creating folders, and batch uploads are supported.

    Method prototype

    go
    func (s *ObjectService) Put(ctx context.Context, key string, r io.Reader, opt *ObjectPutOptions) (*Response, error)
    func (s *ObjectService) PutFromFile(ctx context.Context, name string, filePath string, opt *ObjectPutOptions) (*Response, error)
    

    Sample 1. Uploading an object

    go
    // Sample 1: Using Put to upload an object
    key := "exampleobject"
    f, err := os.Open("../test")
    opt := &cos.ObjectPutOptions{
       ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
           ContentType: "text/html",
       },
       ACLHeaderOptions: &cos.ACLHeaderOptions{
           // Considering the ACL limit, we recommend not setting an object ACL when uploading an object unless required. The object will then inherit the bucket ACL by default.
           XCosACL: "private",
       },
    }
    _, err = client.Object.Put(context.Background(), key, f, opt)
    if err != nil {
       panic(err)
    }
    // Sample 2: Using PUtFromFile to upload a file to COS
    filepath := "./test"
    _, err = client.Object.PutFromFile(context.Background(), key, filepath, opt)
    if err != nil {
       panic(err)
    }
    

    Sample 2. Creating a folder

    COS uses slashes (/) to separate object paths to simulate the effect of directories. Therefore, you can upload an empty stream and append a slash to its name to create an empty directory in COS.

    go
    // Folder name
    name := "folder/"
    // Transfer an input stream whose size is 0.
    _, err := c.Object.Put(context.Background(), name, strings.NewReader(""), nil)
    if err != nil {
    // ERROR
    }
    

    Sample 3. Uploading an object to a COS directory

    You can upload an object whose name is separated with slashes. In this way, the directory that contains this object will be created automatically. If you need to upload new objects to this COS directory, you can pass the value of this directory to dir.

    go
    dir := "exampledir/"
    filename := "exampleobject"
    key := dir + filename
    f := strings.NewReader("test file")
    _, err = c.Object.Put(context.Background(), key, f, nil)
    if err != nil {
       // ERROR
    }
    

    Sample 4. Viewing the upload progress

    go
    type SelfListener struct {
    }
    // A custom progress callback, which requires the ProgressChangedCallback method to be implemented.
    func (l *SelfListener) ProgressChangedCallback(event *cos.ProgressEvent) {
      switch event.EventType {
      case cos.ProgressDataEvent:
          fmt.Printf("\r[ConsumedBytes/TotalBytes: %d/%d, %d%%]",
                      event.ConsumedBytes, event.TotalBytes, event.ConsumedBytes*100/event.TotalBytes)
      case cos.ProgressFailedEvent:
          fmt.Printf("\nTransfer Failed: %v", event.Err)
      }
    }
    func main() {
      // Initialize
      ...
        // Sample 1: Using the default callback function to view the upload progress
      key := "exampleobject"
      f, err := os.Open("../test")
      opt := &cos.ObjectPutOptions{
          ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
              ContentType: "text/html",
              // Set the default progress callback function.
              Listener:    &cos.DefaultProgressListener{},
          },
          ACLHeaderOptions: &cos.ACLHeaderOptions{
              // Considering the ACL limit, we recommend not setting an object ACL when uploading an object unless required. The object will then inherit the bucket ACL by default.
              XCosACL: "private",
          },
      }
      _, err = client.Object.Put(context.Background(), key, f, opt)
      if err != nil {
          panic(err)
      }
       // Sample 2: Using a custom way to view the upload progress
      opt.Listener = &SelfListener{}
      filepath := "./test"
      _, err = client.Object.PutFromFile(context.Background(), key, filepath, opt)
      if err != nil {
          panic(err)
      }
    }
    

    Sample 5. Uploading objects with multiple threads

    
    func upload(wg *sync.WaitGroup, c *cos.Client, files <-chan string) {
      defer wg.Done()
      for file := range files {
          name := "folder/" + file
    fd, err := os.Open(file)
          if err != nil {
              //ERROR
              continue
          }
          _, err = c.Object.Put(context.Background(), name, fd, nil)
          if err != nil {
              //ERROR
          }
      }
    }
    func main() {
          u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
          b := &cos.BaseURL{BucketURL: u}
          c := cos.NewClient(b, &http.Client{
                  Transport: &cos.AuthorizationTransport{
                          SecretID:  os.Getenv("COS_SECRETID"),
                          SecretKey: os.Getenv("COS_SECRETKEY"),
                  },
          })
    // Upload files with multiple threads
          filesCh := make(chan string, 2)
          filePaths := []string{"test1", "test2", "test3"}
          var wg sync.WaitGroup
          threadpool := 2
          for i := 0; i < threadpool; i++ {
                  wg.Add(1)
                  go upload(&wg, c, filesCh)
          }
          for _, filePath := range filePaths {
                  filesCh <- filePath
          }
          close(filesCh)
          wg.Wait()
    }
    

    Parameter description

    go
    type ObjectPutOptions struct {
    *ACLHeaderOptions       
    *ObjectPutHeaderOptions 
    }
    type ACLHeaderOptions struct {
    XCosACL              string                           
      XCosGrantRead        string
      XCosGrantWrite       string 
      XCosGrantFullControl string                                           
    } 
    type ObjectPutHeaderOptions struct {
    CacheControl       string 
    ContentDisposition string 
    ContentEncoding    string 
    ContentType        string 
    ContentLength      int64  
    Expires            string 
    // Custom x-cos-meta-* header
    XCosMetaXXX        *http.Header 
    XCosStorageClass   string      
    XCosTrafficLimit   int
    Listener           ProgressListener
    }
    
    Parameter Description Type Required
    r The content of the uploaded file, which can be a file stream or a byte stream. When r is not bytes.Buffer/bytes.Reader/strings.Reader, opt.ObjectPutHeaderOptions.ContentLength must be specified. io.Reader Yes
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    XCosACL Sets the file ACL, such as private, public-read, and public-read-write. string No
    XCosGrantFullControl Grants full permission in the format: id="[OwnerUin]" string No
    XCosGrantRead Grants read permission in the format: id="[OwnerUin]" string No
    XCosStorageClass Sets the object storage class. Valid values: STANDARD (default), STANDARD_IA, ARCHIVE. string No
    Expires Sets Content-Expires. string No
    CacheControl Cache policy. Sets Cache-Control. string No
    ContentType Content Type. Sets Content-Type. string No
    ContentDisposition File name. Sets Content-Disposition. string No
    ContentEncoding Encoding format. Sets Content-Encoding. string No
    ContentLength Sets the length of the request content. int64 No
    XCosMetaXXX User-defined file metadata. It must start with x-cos-meta. Otherwise, it will be ignored. http.Header No
    XCosTrafficLimit Limits the speed for a single URL. int No
    Listener Progress callback API Struct No

    Response description

    go
    {
      'ETag': 'string',
      'x-cos-expiration': 'string'
    }
    

    The result can be obtained from the response.

    go
    resp, err := client.Object.Put(context.Background(), key, f, nil)
    etag := resp.Header.Get("ETag")
    exp := resp.Header.Get("x-cos-expiration")
    
    Parameter Description Type
    ETag MD5 checksum of the uploaded file string
    x-cos-expiration Returns the file expiration rule if a lifecycle is configured. string

    Querying object metadata

    API description

    The API is used to query object metadata.

    Method prototype

    go
    func (s *ObjectService) Head(ctx context.Context, key string, opt *ObjectHeadOptions) (*Response, error)
    

    Sample request

    go
    key := "exampleobject"
    _, err := client.Object.Head(context.Background(), key, nil)
    if err != nil {
       panic(err)
    }
    

    Parameter description

    go
    type ObjectHeadOptions struct {
    IfModifiedSince string 
    }
    
    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    IfModifiedSince Returned only if the object has been modified after the specified time string No

    Response description

    go
    {
      'Content-Type': 'application/octet-stream',
      'Content-Length': '16807',
      'ETag': '"9a4802d5c99dafe1c04da0a8e7e166bf"',
      'Last-Modified': 'Wed, 28 Oct 2014 20:30:00 GMT',
      'X-Cos-Request-Id': 'NTg3NzQ3ZmVfYmRjMzVfMzE5N182NzczMQ=='
    }
    

    The result can be obtained from the response.

    go
    resp, err := client.Object.Head(context.Background(), key, nil)
    contentType := resp.Header.Get("Content-Type")
    contentLength := resp.Header.Get("Content-Length")
    etag := resp.Header.Get("ETag")
    reqid := resp.Header.Get("X-Cos-Request-Id")
    
    Parameter Description Type
    Metadata Includes both predefined object metadata, including Etag and X-Cos-Request-Id, and custom object metadata. string

    Downloading an object

    API description

    This API (GET Object) is used to download an object (file) to a local file system. Simple downloads and batch downloads are supported.

    Method prototype

    go
    func (s *ObjectService) Get(ctx context.Context, key string, opt *ObjectGetOptions) (*Response, error)
    func (s *ObjectService) GetToFile(ctx context.Context, key, localfile string, opt *ObjectGetOptions) (*Response, error)
    

    Sample 1. Downloading an object

    go
    key := "exampleobject"
    opt := &cos.ObjectGetOptions{
       ResponseContentType: "text/html",
       Range:               "bytes=0-3",
    }
    // "opt" is optional. It can be set to “nil” unless otherwise specified.
    // 1. Obtain the object from the response body
    resp, err := client.Object.Get(context.Background(), key, opt)
    if err != nil {
       panic(err)
    }
    ioutil.ReadAll(resp.Body)
    resp.Body.Close()
    // 2. Download the object locally
    _, err = client.Object.GetToFile(context.Background(), key, "example.txt", nil)
    if err != nil {
       panic(err)
    }
    

    Sample 2. Downloading objects with multiple threads

    go
    func download(wg *sync.WaitGroup, c *cos.Client, keysCh <-chan string) {
           defer wg.Done()
           for key := range keysCh {
                   // Download the file to the current directory. Note that the filename does not contain its directory.
                   _, filename := filepath.Split(key)
                   _, err := c.Object.GetToFile(context.Background(), key, filename, nil)
                   if err != nil {
                       // ERROR
                   }
           }
    }
    func main() {
           u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
           b := &cos.BaseURL{BucketURL: u}
           c := cos.NewClient(b, &http.Client{
                   Transport: &cos.AuthorizationTransport{
                           SecretID:  os.Getenv("COS_SECRETID"),
                           SecretKey: os.Getenv("COS_SECRETKEY"),
                   },
           })
           keysCh := make(chan string, 2)
           keys := []string{"folder/exampleobject1", "folder/exampleobject2", "folder/exampleobject3"}
           var wg sync.WaitGroup
           threadpool := 2
           for i := 0; i < threadpool; i++ {
                   wg.Add(1)
                   go download(&wg, c, keysCh)
           }
           for _, key := range keys {
                   keysCh <- key
           }
           close(keysCh)
           wg.Wait()
    }
    

    Sample 3. Obtaining the download progress

    The Go SDK allows you to obtain the download progress via a callback. You need to implement the cos.ProgressListener API, which is defined as follows:

    go
    const (
      // Data transfer starts.
      ProgressStartedEvent ProgressEventType = iota
      // Transferring data
      ProgressDataEvent
      // Data transfer is completed, which does not mean the completion of the API call.
      ProgressCompletedEvent
      // Returned only when an error occurs during data transfer.
      ProgressFailedEvent
    )
    type ProgressEvent struct {
      EventType     ProgressEventType
      RWBytes       int64  // Number of bytes read at a time
      ConsumedBytes int64  // Number of bytes completed
      TotalBytes    int64  // Total number of bytes
      Err           error  // Error
    }
    type ProgressListener interface {
      ProgressChangedCallback(event *ProgressEvent)
    }
    
    go
    type SelfListener struct {
    }
    // A custom progress callback, which requires the ProgressChangedCallback method to be implemented.
    func (l *SelfListener) ProgressChangedCallback(event *cos.ProgressEvent) {
       switch event.EventType {
       case cos.ProgressDataEvent:
           fmt.Printf("\r[ConsumedBytes/TotalBytes: %d/%d, %d%%]",
                       event.ConsumedBytes, event.TotalBytes, event.ConsumedBytes*100/event.TotalBytes)
       case cos.ProgressFailedEvent:
           fmt.Printf("\nTransfer Failed: %v", event.Err)
       }
    }
    func main() {
       // Initialize
       ... 
        key := "exampleobject"
       opt := &cos.ObjectGetOptions{
           ResponseContentType: "text/html",
           // View the progress using the default progress listener.
    Listener: &cos.DefaultProgressListener{},
       }
       // "opt" is optional. It can be set to “nil” unless otherwise specified.
       // 1. Obtain the object from the response body
       resp, err := client.Object.Get(context.Background(), key, opt)
       if err != nil {
           panic(err)
       }
       ioutil.ReadAll(resp.Body)
       resp.Body.Close()
        // 2. Download the object locally
       // Use the custom progress callback method.
       opt.Listener = &SelfListener{}
       _, err = client.Object.GetToFile(context.Background(), key, "example.txt", opt)
       if err != nil {
           panic(err)
       }
    }
    

    Parameter description

    go
    type ObjectGetOptions struct {
    ResponseContentType        string 
    ResponseContentLanguage    string 
    ResponseExpires            string 
    ResponseCacheControl       string 
    ResponseContentDisposition string 
    ResponseContentEncoding    string 
    Range                      string 
    IfModifiedSince            string 
    XCosTrafficLimit           int
    Listener                   ProgressListener
    }
    
    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    localfile Name of the file downloaded to the local file system string Yes
    ResponseContentType Sets the Content-Type in the response header string No
    ResponseContentLanguage Sets the Content-Language in the response header string No
    ResponseExpires Sets the Content-Expires in the response header string No
    ResponseCacheControl Sets the Cache-Control in the response header string No
    ResponseContentDisposition Sets the Content-Disposition in the response header string No
    ResponseContentEncoding Sets the Content-Encoding in the response header string No
    Range Sets the range of bytes for the file to download in the format of bytes=first-last string No
    IfModifiedSince Returned only if the object has been modified after the specified time string No
    XCosTrafficLimit Limits the speed for a single URL int No
    Listener Progress callback API Struct No

    Response description

    go
    {
      'Body': '',
      'Accept-Ranges': 'bytes',
      'Content-Type': 'application/octet-stream',
      'Content-Length': '16807',
      'Content-Disposition': 'attachment; filename="filename.jpg"',
      'Content-Range': 'bytes 0-16086/16087',
      'ETag': '"9a4802d5c99dafe1c04da0a8e7e166bf"',
      'Last-Modified': 'Wed, 28 Oct 2014 20:30:00 GMT',
      'X-Cos-Request-Id': 'NTg3NzQ3ZmVfYmRjMzVfMzE5N182NzczMQ=='
    }
    

    The result can be obtained from the response.

    go
    resp, err := client.Object.Get(context.Background(), key, nil)
    body, _ := ioutil.ReadAll(resp.Body)
    contentType := resp.Header.Get("Content-Type")
    contentLength := resp.Header.Get("Content-Length")
    etag := resp.Header.Get("ETag")
    reqid := resp.Header.Get("X-Cos-Request-Id")
    
    Parameter Description Type
    Body The content of the downloaded file StreamBody
    File meta information The meta information of the downloaded file, including Etag and X-Cos-Request-Id. The meta information of the configured file is also returned. string

    Copying an object

    This API (PUT Object-Copy) is used to copy files to the destination path.

    Method prototype

    go
    func (s *ObjectService) Copy(ctx context.Context, key, sourceURL string, opt *ObjectCopyOptions) (*ObjectCopyResult, *Response, error)
    

    Sample 1. Copying an object

    go
    name := "exampleobject"
    // Upload the source object
    f := strings.NewReader("test")
    _, err := client.Object.Put(context.Background(), name, f, nil)
    assert.Nil(s.T(), err, "Test Failed")
    sourceURL := fmt.Sprintf("%s/%s", client.BaseURL.BucketURL.Host, name)
    dest := "example_dest"
    // Considering the ACL limit, we recommend not setting an object ACL when uploading an object unless required. The object will then inherit the bucket ACL by default.
    // opt := &cos.ObjectCopyOptions{}
    _, _, err = client.Object.Copy(context.Background(), dest, sourceURL, nil)
    if err != nil {
       panic(err)
    }
    

    Sample 2. Moving an object

    go
    source := "test/oldfile"
    f := strings.NewReader("test")
    // Upload the object.
    _, err := c.Object.Put(context.Background(), source, f, nil)
    if err != nil {
       // Error
    }
    // Move the object.
    dest := "test/newfile"
    soruceURL := fmt.Sprintf("%s/%s", u.Host, source)
    _, _, err := c.Object.Copy(context.Background(), dest, soruceURL, nil)
    if err == nil {
        _, err = c.Object.Delete(context.Background(), source, nil)
        if err != nil {
            // Error
        }
    }
    

    Parameter description

    go
    type ObjectCopyOptions struct {
    *ObjectCopyHeaderOptions 
    *ACLHeaderOptions        
    }
    type ACLHeaderOptions struct {
    XCosACL              string 
    XCosGrantRead        string 
    XCosGrantWrite       string 
    XCosGrantFullControl string 
    }
    type ObjectCopyHeaderOptions struct {
    XCosMetadataDirective           string 
    XCosCopySourceIfModifiedSince   string 
    XCosCopySourceIfUnmodifiedSince string 
    XCosCopySourceIfMatch           string 
    XCosCopySourceIfNoneMatch       string 
    XCosStorageClass                string 
    // Custom x-cos-meta-* header
    XCosMetaXXX                        *http.Header 
    XCosCopySource                     string      
    }
    
    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    sourceURL URL of the source file to copy string Yes
    XCosACL Sets the file ACL, such as private, public-read, and public-read-write string No
    XCosGrantFullControl Grants full permission in the format: id="[OwnerUin]" string No
    XCosGrantRead Grants the read permission in the format: id="[OwnerUin]" string No
    XCosMetadataDirective Valid values:
  • Copy: copies the source object along with its metadata.
  • Replaced: copies the source object while replacing its metadata.
    If the destination path is the same as the source path, this parameter must be set to Replaced.
  • string Yes
    XCosCopySourceIfModifiedSince The operation is performed if the object is modified after the specified time; otherwise, error code 412 is returned. It can be used with XCosCopySourceIfNoneMatch. Using it with other conditions can cause a conflict. string No
    XCosCopySourceIfUnmodifiedSince The operation is performed if the object is unmodified after the specified time; otherwise, error code 412 is returned. It can be used with XCosCopySourceIfMatch. Using it with other conditions can cause a conflict. string No
    XCosCopySourceIfMatch The operation is performed if the Etag of the object is the same as the specified one, otherwise error code 412 is returned. It can be used with XCosCopySourceIfUnmodifiedSince. Using it with other conditions can cause a conflict. string No
    XCosCopySourceIfNoneMatch The operation is performed if the Etag of the object is different from the specified one, otherwise error code 412 is returned. It can be used with XCosCopySourceIfModifiedSince. Using it with other conditions can cause a conflict. string No
    XCosStorageClass Sets the object storage class. Valid values: STANDARD (default), STANDARD_IA, ARCHIVE. string No
    XCosMetaXXX User-defined file metadata. http.Header No
    XCosCopySource Source file URL. You can specify a historical version using the versionid sub-resource. string No

    Response description

    Attributes of the uploaded file:

    go
    type ObjectCopyResult struct {
      ETag         string 
      LastModified string
    }
    
    Parameter Description Type
    ETag MD5 of the copied file string
    LastModified The last time that the copied file was modified string

    Deleting a single object

    API description

    This API is used to delete an object (folder) from a bucket.

    Method prototype

    go
    func (s *ObjectService) Delete(ctx context.Context, key string) (*Response, error)
    

    Sample 1. Deleting an object

    go
    key := "exampleobject"
    _, err := client.Object.Delete(context.Background(), key)
    if err != nil {
       panic(err)
    }
    

    Sample 2. Deleting a folder

    This request does not delete objects in the folder but only the specified key.

    go
    key := "folder/"
    _, err := c.Object.Delete(context.Background(), key)
    if err != nil {
      panic(err)
    }
    

    Parameter description

    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes

    Deleting multiple objects

    API description

    The API is used to delete multiple objects from a bucket. You can delete up to 1,000 objects in one request.

    Method prototype

    go
    func (s *ObjectService) DeleteMulti(ctx context.Context, opt *ObjectDeleteMultiOptions) (*ObjectDeleteMultiResult, *Response, error)
    

    Sample 1. Deleting multiple specified objects

    go
    var objects []string
    objects = append(objects, []string{"a", "b", "c"}...)
    obs := []cos.Object{}
    for _, v := range objects {
       obs = append(obs, cos.Object{Key: v})
    }
    opt := &cos.ObjectDeleteMultiOptions{
       Objects: obs,
       // Boolean, which indicates whether to enable Quiet or Verbose mode.
       // “true” indicates the Quiet mode while “false” (default) indicates the Verbose mode.
       // Quiet: true,
    }
    _, _, err := client.Object.DeleteMulti(context.Background(), opt)
    if err != nil {
       panic(err)
    }
    

    Sample 2. Deleting a folder and the objects contained

    COS uses slashes (/) to separate object paths to simulate the effect of a file system. Therefore, deleting a folder in COS means deleting all objects that have a specified prefix. For example, the folder 'prefix/' contains all objects prefixed with 'prefix/'. In other words, you can delete all objects prefixed with 'prefix/' to delete the 'prefix/' folder.
    Currently, COS’s Go SDK did not provide an API to perform this operation. However, you can still use a combination of basic operations to do so.

    go
    dir := "exampledir/"
    var marker string
    opt := &cos.BucketGetOptions{
      Prefix:  dir,
      MaxKeys: 1000,
    }
    isTruncated := true
    for isTruncated {
      opt.Marker = marker
      v, _, err := c.Bucket.Get(context.Background(), opt)
      if err != nil {
          // Error
          break
      }
      for _, content := range v.Contents {
          _, err = c.Object.Delete(context.Background(), content.Key)
          if err != nil {
              // Error
          }
      }
      isTruncated = v.IsTruncated
      marker = v.NextMarker
    }
    

    Parameter description

    go
    type ObjectDeleteMultiOptions struct {
    Quiet   bool
    Objects []Object   
    }
    // “Object” stores object metadata
    type Object struct {
    Key          string
      // Other parameters are not related to this API
    }
    
    Parameter Description Type Required
    Quiet Indicates whether to enable Quiet or Verbose mode for the response result. Valid values:
  • true: enables the Verbose mode that returns the deletion result for each object
  • false (default): enables the Quiet mode that only returns information on objects that fail
  • Boolean No
    Objects Describes each destination object to be deleted Container Yes
    Key Name of the destination object String Yes

    Response description

    Attributes of the uploaded file:

    go
    // “ObjectDeleteMultiResult” saves the DeleteMulti result
    type ObjectDeleteMultiResult struct {    
    DeletedObjects []Object
    Errors         []struct {
    Key     string
    Code    string
    Message string
    }
    }
    
    Parameter Description Type
    DeletedObjects Describes each object deleted Container
    Errors Describes objects that failed to be deleted Container
    Key Names of the objects that failed to be deleted string
    Code Error code for the deletion failure string
    Message Error message for the deletion failure string

    Restoring an archived object

    API description

    This (POST Object restore) API is used to restore an archived object for access.

    Method prototype

    go
    func (s *ObjectService) PostRestore(ctx context.Context, key string, opt *ObjectRestoreOptions) (*Response, error) 
    

    Sample request

    go
    key := "example_restore"
    f, err := os.Open("../test")
    if err != nil {
       panic(err)
    }
    opt := &cos.ObjectPutOptions{
       ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
           ContentType:      "text/html",
           XCosStorageClass: "ARCHIVE", //ARCHIVE storage class
       },
       ACLHeaderOptions: &cos.ACLHeaderOptions{
           // Considering the ACL limit, we recommend not setting an object ACL when uploading an object unless required. The object will then inherit the bucket ACL by default.
           XCosACL: "private",
       },
    }
    // Upload an object directly to ARCHIVE storage class
    _, err = client.Object.Put(context.Background(), key, f, opt)
    if err != nil {
       panic(err)
    }
    opts := &cos.ObjectRestoreOptions{
       Days: 2,
       Tier: &cos.CASJobParameters{
           // Standard, Expedited, and Bulk
           Tier: "Expedited",
       },
    }
    // Restore an archived object.
    _, err = client.Object.PostRestore(context.Background(), key, opts)
    if err != nil {
       panic(err)
    }
    

    Parameter description

    go
    type ObjectRestoreOptions struct {        
      Days    int               
      Tier    *CASJobParameters 
    }
    type CASJobParameters struct {
      Tier    string 
    }
    
    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    ObjectRestoreOptions Describes rules for retrieved temporary files struct Yes
    Days Specifies the number of days before a temporary object expires int Yes
    CASJobParameters Specifies the restoration configuration struct No
    Tier Object restoration mode. For ARCHIVE, valid values are Expedited, Standard, and Bulk. For DEEP ARCHIVE, valid values are Standard and Bulk string No

    Multipart Operations

    Querying multipart upload operations

    API description

    This API is used to query in-progress multipart uploads in a specified bucket.

    Method prototype

    go
    func (s *BucketService) ListMultipartUploads(ctx context.Context, opt *ListMultipartUploadsOptions) (*ListMultipartUploadsResult, *Response, error)
    

    Sample request

    go
    _, _, err := client.Bucket.ListMultipartUploads(context.Background(), nil)
    if err != nil {
       panic(err)
    }
    

    Parameter description

    go
    type ListMultipartUploadsOptions struct {
    Delimiter      string
    EncodingType   string
    Prefix         string
    MaxUploads     int
    KeyMarker      string
    UploadIDMarker string                                         
    }
    
    Parameter Description Type Required
    Delimiter A symbol. The identical paths between Prefix and the first occurrence of the Delimiter are grouped and defined as a common prefix. If no Prefix is specified, the common prefix starts with the beginning of the path string No
    encodingType Specifies the encoding type of returned values; valid value: url string No
    prefix Specifies that returned object keys must be prefixed with this value. Note that if you use this parameter, returned keys will contain the prefix string No
    MaxUploads Sets the maximum number of multipart uploads that can be returned at a time. Value range: 1−1000. Defaults to 1000. int No
    KeyMarker This parameter is used together with upload-id-marker:
  • If upload-id-marker is not specified, multipart uploads whose ObjectName is lexicographically greater than key-marker will be listed.
  • If upload-id-marker is specified, multipart uploads whose ObjectName is lexicographically greater than key-marker will be listed, and multipart uploads whose ObjectName is lexicographically equal to key-marker with UploadID greater than upload-id-marker will be listed.
  • string No
    UploadIDMarker This parameter is used together with key-marker:
  • If key-marker is not specified, upload-id-marker will be ignored.
  • If key-marker is specified, multipart uploads whose ObjectName is lexicographically greater than key-marker will be listed, and multipart uploads whose ObjectName is lexicographically equal to key-marker with UploadID greater than upload-id-marker will be listed.
  • string No

    Response description

    go
    // ListMultipartUploadsResult saves ListMultipartUploads results
    type ListMultipartUploadsResult struct {
    Bucket             string
    EncodingType       string
    KeyMarker          string
    UploadIDMarker     string
      NextKeyMarker      string
      NextUploadIDMarker string
      MaxUploads         int
      IsTruncated        bool
      Uploads            []struct {
          Key          string
          UploadID     string
          StorageClass string
          Initiator    *Initiator
          Owner        *Owner
          Initiated    string
      }
      Prefix         string
      Delimiter      string
      CommonPrefixes []string 
    }
    // Use the same type as the owner
    type Initiator Owner
    // “Owner” defines the bucket/object's owner
    type Owner struct {
    ID          string
    DisplayName string
    }
    
    Parameter Description Type
    Bucket Destination bucket for multipart upload in the format of BucketName, e.g. examplebucket-1250000000 string
    EncodingType Specifies the encoding method of the returned value. It is left empty by default. Valid value: url string
    KeyMarker Specifies the key after which the listing should begin string
    UploadIDMarker Specifies the uploadId after which the listing should begin string
    NextKeyMarker Specifies the key after which the next listing should begin if the returned list is truncated string
    NextUploadIdMarker Specifies the uploadId after which the next listing should begin if the returned list is truncated string
    MaxUploads The maximum number of returned multipart uploads. It defaults to 1000. string
    IsTruncated Indicates whether the returned list is truncated bool
    Uploads Information on each upload Container
    Key Object name string
    UploadID ID that identifies the multipart upload string
    Key Indicates whether the returned list is truncated bool
    StorageClass Specifies the storage class for parts. Enumerated values: STANDARD, STANDARD_IA, ARCHIVE string
    Initiator Indicates information about the initiator of this upload Container
    Owner Indicates information about the owner of these parts Container
    Initiated Start time of the multipart upload string
    Prefix Specifies that returned object keys must be prefixed with this value. Note that if you use this parameter, returned keys will contain the prefix struct
    Delimiter A symbol. The identical paths between Prefix and the first occurrence of the Delimiter are grouped and defined as a common prefix. If no Prefix is specified, the common prefix starts with the beginning of the path string
    CommonPrefixes The identical paths between Prefix and Delimiter are grouped and defined as a common prefix string
    ID Unique CAM ID of users string
    DisplayName User Identifier (UIN) string

    Multipart upload operations

    Operations related to multipart upload include the following:

    • Multipart upload: initializing a multipart upload operation, uploading parts, and completing a multipart upload operation
    • Deleting uploaded parts.
    Note:

    To perform a multipart upload operation, you can also use the advanced upload API (recommended).

    Initializing a multipart upload

    API description

    This API (Initiate Multipart Upload) is used to initialize a multipart upload operation and get its UploadID.

    Method prototype

    go
    func (s *ObjectService) InitiateMultipartUpload(ctx context.Context, name string, opt *InitiateMultipartUploadOptions) (*InitiateMultipartUploadResult, *Response, error)
    

    Sample request

    go
    name := "exampleobject"
    // Optional. Considering the ACL limit, we recommend not setting an object ACL when uploading an object unless required. The object will then inherit bucket ACL by default.
    v, _, err := client.Object.InitiateMultipartUpload(context.Background(), name, nil)
    if err != nil {
       panic(err)
    }
    UploadID = v.UploadID
    

    Parameter description

    go
    type InitiateMultipartUploadOptions struct {
    *ACLHeaderOptions       
    *ObjectPutHeaderOptions 
    }
    type ACLHeaderOptions struct {
    XCosACL              string                           
    XCosGrantRead        string
    XCosGrantWrite       string 
    XCosGrantFullControl string                                           
    } 
    type ObjectPutHeaderOptions struct {
    CacheControl       string 
    ContentDisposition string 
    ContentEncoding    string 
    ContentType        string 
    ContentLength      int64   
    Expires            string 
    // Custom x-cos-meta-* header
    XCosMetaXXX        *http.Header 
    XCosStorageClass   string      
    }
    
    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    XCosACL Sets the file ACL, such as private,public-read string No
    XCosGrantFullControl Grants full permission in the format: id="[OwnerUin]" string No
    XCosGrantRead Grants read permission in the format: id="[OwnerUin]" string No
    XCosStorageClass Sets the object storage class. Valid values: STANDARD (default), STANDARD_IA, ARCHIVE. string No
    Expires Sets Content-Expires. string No
    CacheControl Cache policy. Sets Cache-Control. string No
    ContentType Content Type. Sets Content-Type. string No
    ContentDisposition File name. Sets Content-Disposition. string No
    ContentEncoding Encoding format. Sets Content-Encoding. string No
    ContentLength Sets the length of the request content. int64 No
    XCosMetaXXX User-defined file metadata. It must start with x-cos-meta. Otherwise, it will be ignored. http.Header No

    Response description

    go
    type InitiateMultipartUploadResult struct {
    Bucket   string
    Key      string
    UploadID string
    } 
    
    Parameter Description Type
    UploadId ID that identifies the multipart upload string
    Bucket Bucket name in the format: BucketName-APPID string
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string

    Uploading a part

    This API is used to upload a part in a multipart upload.

    Method prototype

    go
    func (s *ObjectService) UploadPart(ctx context.Context, key, uploadID string, partNumber int, r io.Reader, opt *ObjectUploadPartOptions) (*Response, error)
    

    Sample request

    go
    // Note: Up to 10,000 parts can be uploaded.
    key := "exampleobject"
    f := strings.NewReader("test hello")
    // "opt" is optional.
    resp, err := client.Object.UploadPart(
       context.Background(), key, UploadID, 1, f, nil,
    )
    if err != nil {
       panic(err)
    }
    PartETag = resp.Header.Get("ETag")
    

    Parameter description

    go
    type ObjectUploadPartOptions struct {
      ContentLength   int64
    }
    
    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    UploadId ID that identifies the multipart upload; generated by InitiateMultipartUpload string Yes
    PartNumber Number that identifies the uploaded part int Yes
    r The content of the uploaded part, which can be a local file stream or an input stream. When r is not bytes.Buffer/bytes.Reader/strings.Reader, opt.ContentLength must be specified. io.Reader Yes
    ContentLength Sets the length of the request content. int64 No

    Response description

    go
    {
      'ETag': 'string'
    }
    

    The result can be obtained from the response.

    go
    resp, err := client.Object.UploadPart(context.Background(), key, UploadID, 1, f, nil)
    etag := resp.Header.Get("ETag")
    
    Parameter Description Type
    ETag MD5 of the uploaded part string

    Querying uploaded parts

    API description

    This API (List Parts) is used to query the uploaded parts of a multipart upload.

    Method prototype

    go
    func (s *ObjectService) ListParts(ctx context.Context, name, uploadID string, opt *ObjectListPartsOptions) (*ObjectListPartsResult, *Response, error)
    

    Sample request

    go
    key := "exampleobject"
    _, _, err := client.Object.ListParts(context.Background(), key, UploadID, nil)
    if err != nil {
       panic(err)
    }
    

    Parameter description

    go
    type ObjectListPartsOptions struct {
    EncodingType     string
    MaxParts         string
    PartNumberMarker string                                      
    }
    
    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    UploadId ID that identifies the multipart upload; generated by InitiateMultipartUpload string Yes
    EncodingType Specifies the encoding type of the returned value string No
    MaxParts Maximum number of parts to return at a time. Defaults to 1000. string No
    PartNumberMarker By default, entries are listed in UTF-8 binary order starting with the part number after the marker string No

    Response description

    go
    type ObjectListPartsResult struct {
    Bucket               string
    EncodingType         string
    Key                  string
    UploadID             string
    Initiator            *Initiator
    Owner                *Owner
    StorageClass         string
    PartNumberMarker     string
    NextPartNumberMarker string
    MaxParts             string
    IsTruncated          bool
    Parts                []Object
    }
    type Initiator struct {
    UIN         string
    ID          string
    DisplayName string
    }
    type Owner struct {
    UIN         string
    ID          string
    DisplayName string
    }
    type Object struct {
    Key          string
    ETag         string
    Size         int
    PartNumber   int
    LastModified string
    StorageClass string 
    Owner        *Owner
    }
    
    Parameter Description Type
    Bucket Bucket name in the format: BucketName-APPID, e.g. examplebucket-1250000000. string
    EncodingType Specifies the encoding method of the returned value. It is left empty by default. Valid value: url string
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string
    UploadId ID that identifies the multipart upload; generated by InitiateMultipartUpload string
    Initiator Initiator information on the multipart upload, including DisplayName, UIN and ID struct
    Owner Information on the file owner, including DisplayName, UIN and ID struct
    StorageClass Sets the object storage class. Valid values: STANDARD (default), STANDARD_IA, ARCHIVE. string
    PartNumberMarker Specifies the part number after which the listing should begin. It defaults to 0, which means the listing begins with the first part string
    NextPartNumberMarker Specifies the part number after which the next listing should begin int
    MaxParts The maximum number of returned parts. It defaults to 1000. int
    IsTruncated Indicates whether the returned list is truncated bool
    Part Information on the uploaded part, including ETag, PartNumber, Size, and LastModified struct

    Completing a multipart upload

    API description

    This API (Complete Multipart Upload) is used to complete the multipart upload of the entire file.

    Method prototype

    go
    func (s *ObjectService) CompleteMultipartUpload(ctx context.Context, key, uploadID string, opt *CompleteMultipartUploadOptions) (*CompleteMultipartUploadResult, *Response, error)
    

    Sample request

    go
    // Complete the multipart upload.
    key := "exampleobject"
    uploadID := UploadID
    opt := &cos.CompleteMultipartUploadOptions{}
    opt.Parts = append(opt.Parts, cos.Object{
    PartNumber: 1, ETag: PartETag},
    )
    _, _, err := client.Object.CompleteMultipartUpload(
    context.Background(), key, uploadID, opt,
    )
    if err != nil {
    panic(err)
    }
    

    Parameter description

    go
    type CompleteMultipartUploadOptions struct {
    Parts   []Object 
    }
    type Object struct { 
    ETag         string 
    PartNumber   int     
    }
    
    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    UploadId ID that identifies the multipart upload; generated by InitiateMultipartUpload string Yes
    CompleteMultipartUploadOptions ETag and PartNumber for all parts struct Yes

    Response description

    go
    type CompleteMultipartUploadResult struct {
    Location string
    Bucket   string
    Key      string
    ETag     string
    }
    
    Parameter Description Type
    Location URL address string
    Bucket Bucket name in the format: BucketName-APPID, e.g. examplebucket-1250000000. string
    key ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg string
    ETag The unique tag of a merged object. This value does not represent the MD5 checksum of the object content, but is used only to verify the uniqueness of the object as a whole. To verify the object content, you can check the ETag of each part during the upload process string

    Aborting a multipart upload

    API description

    This API (Abort Multipart Upload) is used to abort a multipart upload and delete the uploaded parts.

    Method prototype

    go
    func (s *ObjectService) AbortMultipartUpload(ctx context.Context, key, uploadID string) (*Response, error)
    

    Sample request

    go
    key := "exampleobject"
    // Abort
    _, err := client.Object.AbortMultipartUpload(context.Background(), key, UploadID)
    if err != nil {
       panic(err)
    }
    

    Parameter description

    Parameter Description Type Required
    key ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg string Yes
    UploadId ID that identifies the multipart upload string Yes

    Advanced APIs (Recommended)

    Uploading an object

    API description

    The advanced upload API automatically divides your data into parts based on the size of your file. It’s easier to use, eliminating the need to follow each step of the multipart upload process.

    Method prototype

    go
    func (s *ObjectService) Upload(ctx context.Context, key string, filepath string, opt *MultiUploadOptions) (*CompleteMultipartUploadResult, *Response, error)
    

    Sample request

    go
    key := "exampleobject"
    file := "../test"
    _, _, err := client.Object.Upload(
       context.Background(), key, file, nil,
    )
    if err != nil {
       panic(err)
    }
    

    Parameter description

    go
    type MultiUploadOptions struct {
      OptIni             *InitiateMultipartUploadOptions
      PartSize           int64
      ThreadPoolSize     int
    }
    
    Parameter Description Type Required
    key Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    filepath Name of the local file string Yes
    opt Object attributes Struct No
    OptIni Sets object attributes and ACL. For details, see InitiateMultipartUploadOptions Struct No
    PartSize Size of each part in MB. It is automatically determined if you do not specify it as <= 0 int No
    ThreadPoolSize Size of the thread pool. Default: 1 int No

    Response description

    go
    type CompleteMultipartUploadResult struct {
    Location string
    Bucket   string
    Key      string
    ETag     string
    }
    
    Parameter Description Type
    Location URL address string
    Bucket Bucket name in the format: BucketName-APPID, e.g. examplebucket-1250000000. string
    key ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg string
    ETag The unique tag of a merged object. This value does not represent the MD5 checksum of the object content, but is used only to verify the uniqueness of the object as a whole. To verify the object content, you can check the ETag of each part during the upload process string

    Downloading an object

    API description

    The multipart download API automatically downloads data concurrently with Range according to the object size.

    Method prototype

    go
    func (s *ObjectService) Download(ctx context.Context, name string, filepath string, opt *MultiDownloadOptions) (*Response, error)
    

    Sample request

    go
    key := "exampleobject"
    file := "localfile""
    opt := &cos.MultiDownloadOptions{
    ThreadPoolSize: 5,
    }
    _, err := c.Object.Download(
    context.Background(), key, file, opt,
    )
    if err != nil {
       panic(err)
    }
    

    Parameter description

    go
    type MultiDownloadOptions struct {
      Opt            *ObjectGetOptions
      PartSize       int64
      ThreadPoolSize int
      CheckPoint     bool
      CheckPointFile string
    }
    
    Parameter Description Type Required
    name Object key, the unique identifier of an object in a bucket. For example, if the object endpoint is examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, its object key is doc/pic.jpg string Yes
    filepath Name of the local file string Yes
    opt Object download parameter Struct No
    Opt Request parameter. For more information, please see ObjectGetOptions. Struct No
    PartSize Part size (in MB). If this parameter is not specified or is set to a value smaller than or equal to 0, its value will be automatically determined. int64 No
    ThreadPoolSize Size of the thread pool. Default: 1 int No
    CheckPoint Whether to enable checkpoint restart. Default value: false bool No
    CheckPointFile The path to save the download progress file when checkpoint restart is enabled. The default value is <filepath>.cosresumabletask. When the download is completed, this progress file will be cleared. string No

    Response description

    Parameter Description Type
    *Response HTTP response, through which you can obtain the response status code, response headers, and other information. Struct
    error Error message. If no error occurs, nil will be returned. Struct

    Moving an object

    API description

    Object movement can be implemented using the object copy API and object delete API.

    Sample request

    go
    source := "test/oldfile"
    f := strings.NewReader("test")
    // Upload the object.
    _, err := c.Object.Put(context.Background(), source, f, nil)
    if err != nil {
     // Error
    }
    // Move the object.
    dest := "test/newfile"
    soruceURL := fmt.Sprintf("%s/%s", u.Host, source)
    _, _, err := c.Object.Copy(context.Background(), dest, soruceURL, nil)
    if err == nil {
      _, err = c.Object.Delete(context.Background(), source, nil)
      if err != nil {
          // Error
      }
    }