Skip to content

Commit f339425

Browse files
committed
Added samples for get object and get bucket
1 parent b20d8cb commit f339425

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/samples/getBucketSample.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}

src/samples/getObjectSample.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
"samples/utils"
19+
)
20+
21+
// Demonstrates how to get an object within a bucket. Assumes that the
22+
// target bucket already exists and has the specified file.
23+
// 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+
// Grab the first book defined in utils.BookNames
32+
getObjRequest := models.NewGetObjectRequest(utils.BucketName, utils.BookNames[0])
33+
getObjResponse, err := client.GetObject(getObjRequest)
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
38+
// Verify that the contents of the book were retrieved correctly.
39+
utils.VerifyBookContent(utils.BookNames[0], getObjResponse.Content)
40+
}

0 commit comments

Comments
 (0)