Listing Objects in a Bucket (SDK for Go)
Function
This API lists some or all of the objects in a bucket. You can use parameters such as the prefix, number of returned objects, and start position to list objects that meet specified criteria. The listed objects are ordered alphabetically using the object name.
Restrictions
- A maximum of 1,000 objects can be listed for each API call.
- To list objects in a bucket, you must be the bucket owner or have the required permission (obs:bucket:ListBucket in IAM and ListBucket in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Creating a Custom Bucket Policy.
- The mapping between OBS regions and endpoints must comply with what is listed in Regions and Endpoints.
Method
func (obsClient ObsClient) ListObjects(input *ListObjectsInput) (output *ListObjectsOutput, err error)
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
input |
Yes |
Explanation: Input parameters for listing objects in a bucket. For details, see Table 2. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
Bucket |
string |
Yes |
Explanation: Bucket name Restrictions:
Default value: None |
Marker |
string |
No |
Explanation: Object name to start with when listing objects in a bucket. All objects following this parameter are listed in alphabetical order. Restrictions: This parameter is only used for listing objects with a single version. Value range: The value must contain 1 to 1,024 characters. Default value: None |
Prefix |
string |
No |
Explanation: Name prefix that the objects to be listed must contain Assume that you have the following objects: logs/day1, logs/day2, logs/day3, and ExampleObject.jpg. If you specify logs/ as the prefix, logs/day1, logs/day2, and logs/day3 will be returned. If you leave this parameter blank, all objects in the bucket will be returned. Value range: The value must contain 1 to 1,024 characters. Default value: None |
MaxKeys |
int |
No |
Explanation: The maximum number of objects returned in the response in alphabetical order Value range: The value ranges from 1 to 1000. If the specified value is beyond this range, only 1,000 objects are returned. Default value: 1000 |
Delimiter |
string |
No |
Explanation: Object names are grouped by this parameter, which is often used with Prefix. If a prefix is specified, objects with the same string from the prefix to the first delimiter are grouped into one CommonPrefixes. If no prefix is specified, objects with the same string from the first character to the first delimiter are grouped into one CommonPrefixes. Assume that a bucket has objects abcd, abcde, and bbcde in it. If delimiter is set to d and prefix is set to a, objects abcd and abcde are grouped into a CommonPrefixes with abcd as the prefix. If only delimiter is set to d, objects abcd and abcde are grouped into a CommonPrefixes with abcd as the prefix, and bbcde is grouped separately into another CommonPrefixes with bbcd as the prefix. For a parallel file system, if this parameter is not specified, all the content in the current directory is recursively listed by default, and subdirectories are also listed. In big data scenarios, parallel file systems usually have deep directory levels and each directory has a large number of files. In such case, you are advised to configure [delimiter=/] to list the content in the current directory, but not list subdirectories, thereby improving the listing efficiency. Value range: The value must contain 1 to 1,024 characters. Default value: None |
EncodingType |
string |
No |
Explanation: Encoding method that is applied to some elements in the response. If Delimiter, Marker, Prefix, CommonPrefixes, NextMarker, and Key contain control characters (special characters) that are not supported by the XML 1.0 standard, set this parameter to url. Value range: url Default value: None. If you leave this parameter blank, encoding is not applied to elements. |
Responses
Parameter |
Type |
Description |
---|---|---|
output |
Explanation: Returned parameters for listing objects in a bucket. For details, see Table 4. |
|
err |
error |
Explanation: Error messages returned by the API |
Parameter |
Type |
Description |
---|---|---|
StatusCode |
int |
Explanation: HTTP status code Value range: A status code is a group of digits that can be 2xx (indicating successes) or 4xx or 5xx (indicating errors). It indicates the status of a response. For more information, see Status Code. Default value: None |
RequestId |
string |
Explanation: Request ID returned by the OBS server Default value: None |
ResponseHeaders |
map[string][]string |
Explanation: HTTP response headers Default value: None |
Location |
string |
Explanation: Region where a bucket is located Restrictions: If the used endpoint is obs.myhuaweicloud.com, this parameter is not required. If any other endpoint is used, this parameter is required. Value range: To learn about valid regions and endpoints, see Regions and Endpoints. An endpoint is the request address for calling an API. Endpoints vary depending on services and regions. To obtain the regions and endpoints, contact the enterprise administrator. Default value: If obs.myhuaweicloud.com is used as the endpoint and no region is specified, cn-north-1 (the CN North-Beijing1 region) is used by default. |
Name |
string |
Explanation: Bucket name Restrictions:
Value range: The value must contain 3 to 63 characters. |
Prefix |
string |
Explanation: Object name prefix, which is consistent with that set in the request Assume that you have the following objects: logs/day1, logs/day2, logs/day3, and ExampleObject.jpg. If you specify logs/ as the prefix, logs/day1, logs/day2, and logs/day3 will be returned. If you leave this parameter blank, all objects in the bucket will be returned. Value range: The value must contain 1 to 1,024 characters. Default value: None |
Marker |
string |
Explanation: Object name to start with for listing objects Object name to start with when listing objects in a bucket. All objects following this parameter are listed in alphabetical order. Restrictions: This parameter is only used for listing objects with a single version. Value range: The value must contain 1 to 1,024 characters. Default value: None |
Delimiter |
string |
Explanation: Object names are grouped by this parameter, which is often used with Prefix. If a prefix is specified, objects with the same string from the prefix to the first delimiter are grouped into one CommonPrefixes. If no prefix is specified, objects with the same string from the first character to the first delimiter are grouped into one CommonPrefixes. Assume that a bucket has objects abcd, abcde, and bbcde in it. If delimiter is set to d and prefix is set to a, objects abcd and abcde are grouped into a CommonPrefixes with abcd as the prefix. If only delimiter is set to d, objects abcd and abcde are grouped into a CommonPrefixes with abcd as the prefix, and bbcde is grouped separately into another CommonPrefixes with bbcd as the prefix. For a parallel file system, if this parameter is not specified, all the content in the current directory is recursively listed by default, and subdirectories are also listed. In big data scenarios, parallel file systems usually have deep directory levels and each directory has a large number of files. In such case, you are advised to configure [delimiter=/] to list the content in the current directory, but not list subdirectories, thereby improving the listing efficiency. Value range: The value must contain 1 to 1,024 characters. Default value: None |
MaxKeys |
int |
Explanation: Maximum number of listed objects, which is consistent with that set in the request The maximum number of objects returned in the response in alphabetical order Value range: The value ranges from 1 to 1000. If the specified value is beyond this range, only 1,000 objects are returned. Default value: 1000 |
IsTruncated |
bool |
Explanation: Whether all results are returned in the response. A maximum of 1,000 objects can be listed at a time. If the number of objects is greater than 1,000, the objects beyond 1,000 cannot be returned. Value range: true: Not all results are returned. false: All results are returned. Default value: None |
NextMarker |
string |
Explanation: Where in the bucket the next listing begins. If not all results are returned, the response contains this parameter to mark the last object listed in the request. In a subsequent request, you can set Marker to the value of this parameter to list the remaining objects. Value range: The value must contain 1 to 1,024 characters. Default value: None |
Contents |
[]Content |
Explanation: List of objects in the bucket. For details, see Content. |
CommonPrefixes |
[]string |
Explanation: List of object name prefixes grouped according to the Delimiter parameter (if specified) Value range: The value must contain 1 to 1,024 characters. Default value: None |
EncodingType |
string |
Explanation: Encoding method that is applied to some elements in the response. If Delimiter, Marker, Prefix, CommonPrefixes, NextMarker, and Key contain control characters (special characters) that are not supported by the XML 1.0 standard, set this parameter to url. Value range: url Default value: None |
Parameter |
Type |
Description |
---|---|---|
Key |
string |
Explanation: Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path that does not contain the bucket name. For example, if the address for accessing the object is examplebucket.obs.ap-southeast-1.myhuaweicloud.com/folder/test.txt, the object name is folder/test.txt. Value range: The value must contain 1 to 1,024 characters. Default value: None |
LastModified |
time.Time |
Explanation: Time (UTC) when an object was last modified Value range: UTC time Default value: None |
ETag |
string |
Explanation: Base64-encoded, 128-bit MD5 value of an object. ETag is the unique identifier of the object content. It can be used to determine whether the object content is changed. For example, if the ETag value is A when an object is uploaded, but changes to B when the object is downloaded, it indicates that the object content has been changed. The ETag reflects changes to the object content, rather than the object metadata. An uploaded or copied object has a unique ETag after being encrypted using MD5. Restrictions: If an object is encrypted using server-side encryption, the ETag is not the MD5 value of the object. Value range: The value must contain 32 characters. Default value: None |
Size |
int64 |
Explanation: Object size in bytes Value range: The value ranges from 0 TB to 48.8 TB, in bytes. Default value: None |
Owner |
Explanation: Object owner. This parameter contains the domain ID and name of the object owner. For details, see Table 7. Default value: None |
|
StorageClass |
string |
Explanation: Object storage class Value range: See Table 6. Default value: None |
Constant |
Default Value |
Description |
---|---|---|
StorageClassStandard |
STANDARD |
OBS Standard Features low access latency and high throughput and is used for storing massive, frequently accessed (multiple times a month) or small objects (< 1 MB) requiring quick response. |
StorageClassWarm |
WARM |
OBS Infrequent Access Used for storing data that is semi-frequently accessed (fewer than 12 times a year) but is instantly available when needed. |
StorageClassCold |
COLD |
OBS Archive Used for storing rarely accessed (once a year) data. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
ID |
string |
Yes if used as a request parameter |
Explanation: Account (domain) ID of the owner Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? Default value: None |
Listing Objects
This example lists objects in a bucket. A maximum of 1,000 objects can be returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package main import ( "fmt" "os" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { //Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. ak := os.Getenv("AccessKeyID") sk := os.Getenv("SecretAccessKey") // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding to reduce leakage risks. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // securityToken := os.Getenv("SecurityToken") // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com" // Create an obsClient instance. // If you use a temporary AK/SK pair and a security token to access OBS, use the obs.WithSecurityToken method to specify a security token when creating an instance. obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/) if err != nil { fmt.Printf("Create obsClient error, errMsg: %s", err.Error()) } input := &obs.ListObjectsInput{} // Specify a bucket name. input.Bucket = "examplebucket" // List objects in the bucket. output, err := obsClient.ListObjects(input) if err == nil { fmt.Printf("List objects under the bucket(%s) successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) for index, val := range output.Contents { fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n", index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size) } return } fmt.Printf("List objects under the bucket(%s) fail!\n", input.Bucket) if obsError, ok := err.(obs.ObsError); ok { fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.") fmt.Println(obsError.Error()) } else { fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.") fmt.Println(err) } } |
Listing a Specified Number of Objects
This example lists a specified number of objects in a bucket.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package main import ( "fmt" "os" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. ak := os.Getenv("AccessKeyID") sk := os.Getenv("SecretAccessKey") // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding to reduce leakage risks. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // securityToken := os.Getenv("SecurityToken") // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com" // Create an obsClient instance. // If you use a temporary AK/SK pair and a security token to access OBS, use the obs.WithSecurityToken method to specify a security token when creating an instance. obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/) if err != nil { fmt.Printf("Create obsClient error, errMsg: %s", err.Error()) } input := &obs.ListObjectsInput{} // Specify a bucket name. input.Bucket = "examplebucket" // Specify the maximum number of objects to be returned. 100 is used as an example. Returned objects are listed in alphabetic order. The default value is 1000. input.MaxKeys = 100 // List objects in the bucket. output, err := obsClient.ListObjects(input) if err == nil { fmt.Printf("List objects under the bucket(%s) successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) for index, val := range output.Contents { fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n", index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size) } return } fmt.Printf("List objects under the bucket(%s) fail!\n", input.Bucket) if obsError, ok := err.(obs.ObsError); ok { fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.") fmt.Println(obsError.Error()) } else { fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.") fmt.Println(err) } } |
Listing Objects with a Specified Prefix
This example lists objects with a specified prefix.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package main import ( "fmt" "os" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. ak := os.Getenv("AccessKeyID") sk := os.Getenv("SecretAccessKey") // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding to reduce leakage risks. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // securityToken := os.Getenv("SecurityToken") // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com" // Create an obsClient instance. // If you use a temporary AK/SK pair and a security token to access OBS, use the obs.WithSecurityToken method to specify a security token when creating an instance. obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/) if err != nil { fmt.Printf("Create obsClient error, errMsg: %s", err.Error()) } input := &obs.ListObjectsInput{} // Specify a bucket name. input.Bucket = "examplebucket" // Specify an object prefix (test/ as an example). Objects whose names contain the specified prefix will be listed. input.Prefix = "test/" // Specify the maximum number of objects to be returned. 100 is used as an example. Returned objects are listed in alphabetic order. The default value is 1000. input.MaxKeys = 100 // List objects in the bucket. output, err := obsClient.ListObjects(input) if err == nil { fmt.Printf("List objects under the bucket(%s) successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) for index, val := range output.Contents { fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n", index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size) } return } fmt.Printf("List objects under the bucket(%s) fail!\n", input.Bucket) if obsError, ok := err.(obs.ObsError); ok { fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.") fmt.Println(obsError.Error()) } else { fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.") fmt.Println(err) } } |
Listing All Objects Using Pagination
This example lists all objects using pagination.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
// This example lists all files in bucket examplebucket using pagination. package main import ( "fmt" "os" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. ak := os.Getenv("AccessKeyID") sk := os.Getenv("SecretAccessKey") // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding to reduce leakage risks. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. securityToken := os.Getenv("SecurityToken") // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com" // Create an obsClient instance. // If you use a temporary AK/SK pair and a security token to access OBS, use the obs.WithSecurityToken method to specify a security token when creating an instance. obsClient, err := obs.New(ak, sk, endPoint, obs.WithSecurityToken(securityToken)) if err != nil { fmt.Printf("Create obsClient error, errMsg: %s", err.Error()) } input := &obs.ListObjectsInput{} // Specify a bucket name. input.Bucket = "examplebucket" // Specify the maximum number of objects to be returned. 100 is used as an example. Returned objects are listed in alphabetic order. The default value is 1000. input.MaxKeys = 100 // Specify an encoding type. url is used as an example. If the objects to list contain special characters, this parameter must be passed. input.EncodingType = "url" for { // List objects in the bucket. output, err := obsClient.ListObjects(input) if err != nil { fmt.Printf("List objects under the bucket(%s) fail!\n", input.Bucket) if obsError, ok := err.(obs.ObsError); ok { fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.") fmt.Println(obsError.Error()) } else { fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.") fmt.Println(err) } os.Exit(-1) } // Print the listing result. By default, 100 records are returned at a time. fmt.Printf("List objects under the bucket(%s) successful!\n", input.Bucket) for index, val := range output.Contents { fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n", index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size) } if output.IsTruncated { input.Marker = output.NextMarker } else { break } } } |
Listing All Objects with Folder Name as a Prefix
There is no concept of folders in OBS. All elements stored in OBS buckets are objects. Folders are actually objects whose sizes are 0 and whose names end with a slash (/). You can set a folder name to a prefix to list objects in this folder. Sample code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package main import ( "fmt" "os" obs "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" ) func main() { // Obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. Using hard coding may result in leakage. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. ak := os.Getenv("AccessKeyID") sk := os.Getenv("SecretAccessKey") // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding to reduce leakage risks. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways. // securityToken := os.Getenv("SecurityToken") // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use. endPoint := "https://obs.ap-southeast-1.myhuaweicloud.com" // Create an obsClient instance. // If you use a temporary AK/SK pair and a security token to access OBS, use the obs.WithSecurityToken method to specify a security token when creating an instance. obsClient, err := obs.New(ak, sk, endPoint/*, obs.WithSecurityToken(securityToken)*/) if err != nil { fmt.Printf("Create obsClient error, errMsg: %s", err.Error()) } input := &obs.ListObjectsInput{} // Specify a bucket name. input.Bucket = "examplebucket" // Specify the maximum number of objects to be returned. 100 is used as an example. Returned objects are listed in alphabetic order. The default value is 1000. input.MaxKeys = 100 // Specify a delimiter for grouping object names. A slash (/) is used as an example. input.Delimiter = "/" // List objects in the bucket. output, err := obsClient.ListObjects(input) if err == nil { fmt.Printf("List objects under the bucket(%s) successful!\n", input.Bucket) fmt.Printf("RequestId:%s\n", output.RequestId) for index, val := range output.Contents { fmt.Printf("Content[%d]-OwnerId:%s, ETag:%s, Key:%s, LastModified:%s, Size:%d\n", index, val.Owner.ID, val.ETag, val.Key, val.LastModified, val.Size) } return } fmt.Printf("List objects under the bucket(%s) fail!\n", input.Bucket) if obsError, ok := err.(obs.ObsError); ok { fmt.Println("An ObsError was found, which means your request sent to OBS was rejected with an error response.") fmt.Println(obsError.Error()) } else { fmt.Println("An Exception was found, which means the client encountered an internal problem when attempting to communicate with OBS, for example, the client was unable to access the network.") fmt.Println(err) } } |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot