| 1 |
//go:build example |
| 2 |
// +build example |
| 3 |
|
| 4 |
/* |
| 5 |
* MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 6 |
* Copyright 2015-2021 MinIO, Inc. |
| 7 |
* |
| 8 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
* you may not use this file except in compliance with the License. |
| 10 |
* You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, software |
| 15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
* See the License for the specific language governing permissions and |
| 18 |
* limitations under the License. |
| 19 |
*/ |
| 20 |
|
| 21 |
package main |
| 22 |
|
| 23 |
import ( |
| 24 |
"bytes" |
| 25 |
"context" |
| 26 |
"fmt" |
| 27 |
"log" |
| 28 |
"math/rand" |
| 29 |
"os" |
| 30 |
"time" |
| 31 |
|
| 32 |
"github.com/minio/minio-go/v7" |
| 33 |
"github.com/minio/minio-go/v7/pkg/credentials" |
| 34 |
) |
| 35 |
|
| 36 |
func main() { |
| 37 |
const ( |
| 38 |
// Note: These constants are dummy values, |
| 39 |
// please replace them with values for your setup. |
| 40 |
YOURACCESSKEYID = "Q3AM3UQ867SPQQA43P2F" |
| 41 |
YOURSECRETACCESSKEY = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG" |
| 42 |
YOURENDPOINT = "play.min.io" |
| 43 |
YOURBUCKET = "mybucket" // 'mc mb play/mybucket' if it does not exist. |
| 44 |
) |
| 45 |
|
| 46 |
// Requests are always secure (HTTPS) by default. Set secure=false to enable insecure (HTTP) access. |
| 47 |
// This boolean value is the last argument for New(). |
| 48 |
|
| 49 |
// New returns an Amazon S3 compatible client object. API compatibility (v2 or v4) is automatically |
| 50 |
// determined based on the Endpoint value. |
| 51 |
minioClient, err := minio.New(YOURENDPOINT, &minio.Options{ |
| 52 |
Creds: credentials.NewStaticV4(YOURACCESSKEYID, YOURSECRETACCESSKEY, ""), |
| 53 |
Secure: true, |
| 54 |
}) |
| 55 |
if err != nil { |
| 56 |
log.Fatalln(err) |
| 57 |
} |
| 58 |
minioClient.TraceOn(os.Stdout) |
| 59 |
|
| 60 |
input := make(chan minio.SnowballObject, 1) |
| 61 |
opts := minio.SnowballOptions{ |
| 62 |
Opts: minio.PutObjectOptions{}, |
| 63 |
// Keep in memory. We use this since we have small total payload. |
| 64 |
InMemory: true, |
| 65 |
// Compress data when uploading to a MinIO host. |
| 66 |
Compress: true, |
| 67 |
} |
| 68 |
|
| 69 |
// Generate a shared prefix. |
| 70 |
rng := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 71 |
prefix := []byte("aaaaaaaaaaaaaaa") |
| 72 |
for i := range prefix { |
| 73 |
prefix[i] += byte(rng.Intn(25)) |
| 74 |
} |
| 75 |
|
| 76 |
// Generate |
| 77 |
go func() { |
| 78 |
defer close(input) |
| 79 |
|
| 80 |
// Create 100 objects |
| 81 |
for i := 0; i < 100; i++ { |
| 82 |
// With random size 0 -> 10000 |
| 83 |
size := rng.Intn(10000) |
| 84 |
key := fmt.Sprintf("%s/%d-%d.bin", string(prefix), i, size) |
| 85 |
input <- minio.SnowballObject{ |
| 86 |
// Create path to store objects within the bucket. |
| 87 |
Key: key, |
| 88 |
Size: int64(size), |
| 89 |
ModTime: time.Now(), |
| 90 |
Content: bytes.NewBuffer(make([]byte, size)), |
| 91 |
Close: func() { |
| 92 |
fmt.Println(key, "Close function called") |
| 93 |
}, |
| 94 |
} |
| 95 |
} |
| 96 |
}() |
| 97 |
|
| 98 |
// Collect and upload all entries. |
| 99 |
err = minioClient.PutObjectsSnowball(context.TODO(), YOURBUCKET, opts, input) |
| 100 |
if err != nil { |
| 101 |
log.Fatalln(err) |
| 102 |
} |
| 103 |
// Objects successfully uploaded. |
| 104 |
|
| 105 |
// List the content of the prefix: |
| 106 |
lopts := minio.ListObjectsOptions{ |
| 107 |
Recursive: true, |
| 108 |
Prefix: string(prefix) + "/", |
| 109 |
} |
| 110 |
|
| 111 |
// List all objects from a bucket-name with a matching prefix. |
| 112 |
for object := range minioClient.ListObjects(context.Background(), YOURBUCKET, lopts) { |
| 113 |
if object.Err != nil { |
| 114 |
log.Fatalln(object.Err) |
| 115 |
} |
| 116 |
fmt.Println(object.Key, "Size:", object.Size) |
| 117 |
} |
| 118 |
} |