|
| 1 | +// Copyright 2014-2017 Spectra Logic Corporation. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
| 3 | +// this file except in compliance with the License. A copy of the License is located at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// or in the "license" file accompanying this file. |
| 8 | +// This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 9 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +// specific language governing permissions and limitations under the License. |
| 11 | + |
| 12 | +package main |
| 13 | + |
| 14 | +import ( |
| 15 | + "log" |
| 16 | + "ds3/models" |
| 17 | + "ds3/buildclient" |
| 18 | + "fmt" |
| 19 | + "samples/utils" |
| 20 | +) |
| 21 | + |
| 22 | +// Demonstrates how to get a list of S3 objects in a bucket. Assumes that the target |
| 23 | +// bucket already exists and has files, i.e. run putBulkSample.go first. |
| 24 | +func main() { |
| 25 | + // Create the client from environment variables. |
| 26 | + client, err := buildclient.FromEnv() |
| 27 | + if err != nil { |
| 28 | + log.Fatal(err) |
| 29 | + } |
| 30 | + |
| 31 | + // Create the get bucket request. |
| 32 | + bucketRequest := models.NewGetBucketRequest(utils.BucketName) |
| 33 | + |
| 34 | + // Perform the Get Bucket call by using the client and invoking the desired command. |
| 35 | + bucketResponse, err := client.GetBucket(bucketRequest) |
| 36 | + if err != nil { |
| 37 | + log.Fatal(err) |
| 38 | + } |
| 39 | + |
| 40 | + if len(bucketResponse.ListBucketResult.Objects) == 0 { |
| 41 | + fmt.Printf("There are no objects in bucket '%s'.\n", utils.BucketName) |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + fmt.Printf("Objects in bucket '%s'\n", utils.BucketName) |
| 46 | + for i, obj := range bucketResponse.ListBucketResult.Objects { |
| 47 | + fmt.Printf("%d) Object: '%s' created on '%s'\n", |
| 48 | + i, |
| 49 | + utils.ToSafeString(obj.Key), |
| 50 | + utils.ToSafeString(obj.LastModified)) |
| 51 | + } |
| 52 | +} |
0 commit comments