tencent cloud

Feedback

Querying Object Metadata

Last updated: 2022-05-19 14:50:05

    Overview

    This document provides an overview of APIs and SDK code samples for querying object metadata.

    API Operation Description
    HEAD Object Querying object metadata Queries the metadata of an object.

    Querying Object Metadata

    Feature description

    This API is used to query object metadata.

    Method prototype

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

    Sample request

    package main
    import (
       "context"
       "github.com/tencentyun/cos-go-sdk-v5"
       "net/http"
       "net/url"
       "os"
    )
    func main() {
       // Bucket name in the format of `BucketName-APPID` (`APPID` is required), which can be viewed in the COS console at https://console.cloud.tencent.com/cos5/bucket.
       // Replace it with your `region`, which can be viewed in the COS console at https://console.cloud.tencent.com/. For more information about regions, see https://intl.cloud.tencent.com/document/product/436/6224.
       u, _ := url.Parse("https://examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com")
       b := &cos.BaseURL{BucketURL: u}
       client := cos.NewClient(b, &http.Client{
           Transport: &cos.AuthorizationTransport{
               // Get the key from environment variables
               // Environment variable `SECRETID` refers to the user's `SecretId`, which can be viewed in the CAM console at https://console.cloud.tencent.com/cam/capi.
               SecretID: os.Getenv("SECRETID"),
               // Environment variable `SECRETKEY` refers to the user's `SecretKey`, which can be viewed in the CAM console at https://console.cloud.tencent.com/cam/capi.
               SecretKey: os.Getenv("SECRETKEY"),
           },
       })
       key := "exampleobject"
       _, err := client.Object.Head(context.Background(), key, nil)
       if err != nil {
           panic(err)
       }
    }
    

    Parameter description

    type ObjectHeadOptions struct {
      IfModifiedSince string 
    }
    
    Parameter Description Type Required
    key ObjectKey is the unique identifier of an object in a 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
    IfModifiedSince Returned only if the object is modified after the specified time String No

    Response description

    {
      '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.

    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 Metadata of the queried file, including ETag and X-Cos-Request-Id. The metadata of the configured file is also returned. String
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support