1 Star 0 Fork 0

凡夫俗子1025 / huaweicloud-sdk-go-obs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
list_objects_sample.go 4.75 KB
一键复制 编辑 原始数据 按行查看 历史
redabc 提交于 2021-03-02 21:29 . release 3.21.1 source code for go SDK
// Copyright 2019 Huawei Technologies Co.,Ltd.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
/**
* This sample demonstrates how to list objects under specified bucket
* from OBS using the OBS SDK for Go.
*/
package examples
import (
"fmt"
"obs"
"strconv"
"strings"
)
type ListObjectsSample struct {
bucketName string
location string
obsClient *obs.ObsClient
}
func newListObjectsSample(ak, sk, endpoint, bucketName, location string) *ListObjectsSample {
obsClient, err := obs.New(ak, sk, endpoint)
if err != nil {
panic(err)
}
return &ListObjectsSample{obsClient: obsClient, bucketName: bucketName, location: location}
}
func (sample ListObjectsSample) CreateBucket() {
input := &obs.CreateBucketInput{}
input.Bucket = sample.bucketName
input.Location = sample.location
_, err := sample.obsClient.CreateBucket(input)
if err != nil {
panic(err)
}
fmt.Printf("Create bucket:%s successfully!\n", sample.bucketName)
fmt.Println()
}
func (sample ListObjectsSample) DoInsertObjects() []string {
keyPrefix := "MyObjectKey"
input := &obs.PutObjectInput{}
input.Bucket = sample.bucketName
input.Body = strings.NewReader("Hello OBS")
keys := make([]string, 0, 100)
for i := 0; i < 100; i++ {
input.Key = keyPrefix + strconv.Itoa(i)
_, err := sample.obsClient.PutObject(input)
if err != nil {
panic(err)
}
fmt.Printf("Succeed to put object %s\n", input.Key)
keys = append(keys, input.Key)
}
fmt.Println()
return keys
}
func (sample ListObjectsSample) ListObjects() {
input := &obs.ListObjectsInput{}
input.Bucket = sample.bucketName
output, err := sample.obsClient.ListObjects(input)
if err != nil {
panic(err)
}
for index, val := range output.Contents {
fmt.Printf("Content[%d]-ETag:%s, Key:%s, Size:%d\n",
index, val.ETag, val.Key, val.Size)
}
fmt.Println()
}
func (sample ListObjectsSample) ListObjectsByMarker() {
input := &obs.ListObjectsInput{}
input.Bucket = sample.bucketName
input.MaxKeys = 10
output, err := sample.obsClient.ListObjects(input)
if err != nil {
panic(err)
}
fmt.Println("List the first 10 objects :")
for index, val := range output.Contents {
fmt.Printf("Content[%d]-ETag:%s, Key:%s, Size:%d\n",
index, val.ETag, val.Key, val.Size)
}
fmt.Println()
input.Marker = output.NextMarker
output, err = sample.obsClient.ListObjects(input)
if err != nil {
panic(err)
}
fmt.Println("List the second 10 objects using marker:")
for index, val := range output.Contents {
fmt.Printf("Content[%d]-ETag:%s, Key:%s, Size:%d\n",
index, val.ETag, val.Key, val.Size)
}
fmt.Println()
}
func (sample ListObjectsSample) ListObjectsByPage() {
pageSize := 10
pageNum := 1
input := &obs.ListObjectsInput{}
input.Bucket = sample.bucketName
input.MaxKeys = pageSize
for {
output, err := sample.obsClient.ListObjects(input)
if err != nil {
panic(err)
}
fmt.Printf("Page:%d\n", pageNum)
for index, val := range output.Contents {
fmt.Printf("Content[%d]-ETag:%s, Key:%s, Size:%d\n",
index, val.ETag, val.Key, val.Size)
}
if output.IsTruncated {
input.Marker = output.NextMarker
pageNum++
} else {
break
}
}
fmt.Println()
}
func (sample ListObjectsSample) DeleteObjects(keys []string) {
input := &obs.DeleteObjectsInput{}
input.Bucket = sample.bucketName
objects := make([]obs.ObjectToDelete, 0, len(keys))
for _, key := range keys {
objects = append(objects, obs.ObjectToDelete{Key: key})
}
input.Objects = objects
_, err := sample.obsClient.DeleteObjects(input)
if err != nil {
panic(err)
}
fmt.Println("Delete objects successfully!")
}
func RunListObjectsSample() {
const (
endpoint = "https://your-endpoint"
ak = "*** Provide your Access Key ***"
sk = "*** Provide your Secret Key ***"
bucketName = "bucket-test"
location = "yourbucketlocation"
)
sample := newListObjectsSample(ak, sk, endpoint, bucketName, location)
fmt.Println("Create a new bucket for demo")
sample.CreateBucket()
// First insert 100 objects for demo
keys := sample.DoInsertObjects()
// List objects using default parameters, will return up to 1000 objects
sample.ListObjects()
// List the first 10 and second 10 objects
sample.ListObjectsByMarker()
// List objects in way of pagination
sample.ListObjectsByPage()
// Delete all the objects created
sample.DeleteObjects(keys)
}
Go
1
https://gitee.com/Clorious/huaweicloud-sdk-go-obs.git
git@gitee.com:Clorious/huaweicloud-sdk-go-obs.git
Clorious
huaweicloud-sdk-go-obs
huaweicloud-sdk-go-obs
v3.21.2

搜索帮助