-
Notifications
You must be signed in to change notification settings - Fork 5
/
getBucketSample.go
54 lines (46 loc) · 1.88 KB
/
getBucketSample.go
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
// Copyright 2014-2017 Spectra Logic Corporation. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use
// this file except in compliance with the License. A copy of the License is located at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// or in the "license" file accompanying this file.
// This file 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.
package functions
import (
"log"
"github.com/SpectraLogic/ds3_go_sdk/ds3/models"
"github.com/SpectraLogic/ds3_go_sdk/ds3/buildclient"
"fmt"
"github.com/SpectraLogic/ds3_go_sdk/samples/utils"
)
// Demonstrates how to get a list of S3 objects in a bucket. Assumes that the target
// bucket already exists and has files, i.e. run putBulkSample.go first.
func GetBucketSample() {
fmt.Println("---- Get Bucket Sample ----")
// Create the client from environment variables.
client, err := buildclient.FromEnv()
if err != nil {
log.Fatal(err)
}
// Create the get bucket request.
bucketRequest := models.NewGetBucketRequest(utils.BucketName)
// Perform the Get Bucket call by using the client and invoking the desired command.
bucketResponse, err := client.GetBucket(bucketRequest)
if err != nil {
log.Fatal(err)
}
if len(bucketResponse.ListBucketResult.Objects) == 0 {
fmt.Printf("There are no objects in bucket '%s'.\n", utils.BucketName)
return
}
fmt.Printf("Objects in bucket '%s'\n", utils.BucketName)
for i, obj := range bucketResponse.ListBucketResult.Objects {
fmt.Printf("%d) Object: '%s' created on '%s'\n",
i,
utils.ToSafeString(obj.Key),
utils.ToSafeString(obj.LastModified))
}
}