Skip to content

Commit cd8d455

Browse files
committed
Initial commit
0 parents  commit cd8d455

File tree

6 files changed

+571
-0
lines changed

6 files changed

+571
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Maximilian Schoenenberg
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# go-cloud-ibm-cos
2+
3+
See my post about this: [Accessing IBM Cloud Object Storage using Go Cloud](https://schoenenberg.dev/gocloud-with-ibm-cos/)
4+
5+
To use it enter your credentials in `./bin/list_objects/main.go` and run `go run ./bin/list_objects`.

bin/list_objects/main.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
8+
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/aws/credentials"
10+
"github.com/aws/aws-sdk-go/aws/session"
11+
"gocloud.dev/blob/s3blob"
12+
13+
"github.com/schoenenberg/go-cloud-ibm-cos/pkg/bucketop"
14+
)
15+
16+
// Enter your credentials here!
17+
const (
18+
endpoint = "<public endpoint>"
19+
region = "<location>"
20+
bucketName = "<bucket name>"
21+
apiKey = "<apikey>"
22+
secret = "<secret_access_key>"
23+
keyId = "<access_key_id>"
24+
)
25+
26+
func main() {
27+
ctx := context.Background()
28+
29+
// Create a session with s3 constructor
30+
sess, err := session.NewSession(&aws.Config{
31+
Region: aws.String(region),
32+
Endpoint: aws.String(endpoint),
33+
Credentials: credentials.NewStaticCredentials(
34+
keyId,
35+
secret,
36+
apiKey,
37+
),
38+
})
39+
if err != nil {
40+
log.Fatalln(err)
41+
}
42+
43+
// Open the bucket
44+
bucket, err := s3blob.OpenBucket(
45+
ctx,
46+
sess,
47+
bucketName,
48+
nil,
49+
)
50+
if err != nil {
51+
log.Fatalln(err)
52+
}
53+
defer bucket.Close()
54+
55+
// Use our ListObjects function
56+
objs, err := bucketop.ListObjects(ctx, bucket)
57+
if err != nil {
58+
log.Fatalln(err)
59+
}
60+
61+
// And print all objects with its size
62+
for _, obj := range *objs {
63+
fmt.Printf("%s - Size %.2f MB\n",
64+
obj.Key,
65+
float64(obj.Size)/1000000.0,
66+
)
67+
}
68+
}

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/schoenenberg/go-cloud-ibm-cos
2+
3+
go 1.15
4+
5+
require (
6+
github.com/aws/aws-sdk-go v1.35.0
7+
gocloud.dev v0.20.0
8+
)

0 commit comments

Comments
 (0)