|
| 1 | +// Copyright 2019 Huawei Technologies Co.,Ltd. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 3 | +// this file except in compliance with the License. You may obtain a copy of the |
| 4 | +// License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software distributed |
| 9 | +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 10 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | +// specific language governing permissions and limitations under the License. |
| 12 | + |
| 13 | +/** |
| 14 | + * This sample demonstrates how to use custom http.Client to upload multiparts to OBS |
| 15 | + * using the OBS SDK for Go. |
| 16 | + */ |
| 17 | +package examples |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "obs" |
| 23 | + "strings" |
| 24 | +) |
| 25 | + |
| 26 | +type SimpleCustumHttpClientSample struct { |
| 27 | + bucketName string |
| 28 | + objectKey string |
| 29 | + location string |
| 30 | + obsClient *obs.ObsClient |
| 31 | +} |
| 32 | +type OurCustomTransport struct { |
| 33 | + Transport http.RoundTripper |
| 34 | +} |
| 35 | + |
| 36 | +func (t *OurCustomTransport) transport() http.RoundTripper { |
| 37 | + if t.Transport != nil { |
| 38 | + return t.Transport |
| 39 | + } |
| 40 | + return http.DefaultTransport |
| 41 | +} |
| 42 | +func (t *OurCustomTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 43 | + fmt.Println("do someting") |
| 44 | + return t.transport().RoundTrip(req) |
| 45 | +} |
| 46 | + |
| 47 | +func newCustumHttpClientSample(ak, sk, endpoint, bucketName, objectKey, location string) *SimpleCustumHttpClientSample { |
| 48 | + |
| 49 | + t := &http.Client{ |
| 50 | + Transport: &OurCustomTransport{}, |
| 51 | + } |
| 52 | + obsClient, err := obs.New(ak, sk, endpoint, obs.WithHttpClient(t)) |
| 53 | + if err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + return &SimpleCustumHttpClientSample{obsClient: obsClient, bucketName: bucketName, objectKey: objectKey, location: location} |
| 57 | +} |
| 58 | + |
| 59 | +func (sample SimpleCustumHttpClientSample) CreateBucket() { |
| 60 | + input := &obs.CreateBucketInput{} |
| 61 | + input.Bucket = sample.bucketName |
| 62 | + input.Location = sample.location |
| 63 | + _, err := sample.obsClient.CreateBucket(input) |
| 64 | + if err != nil { |
| 65 | + panic(err) |
| 66 | + } |
| 67 | + fmt.Printf("Create bucket:%s successfully!\n", sample.bucketName) |
| 68 | + fmt.Println() |
| 69 | +} |
| 70 | + |
| 71 | +func (sample SimpleCustumHttpClientSample) InitiateMultipartUpload() string { |
| 72 | + input := &obs.InitiateMultipartUploadInput{} |
| 73 | + input.Bucket = sample.bucketName |
| 74 | + input.Key = sample.objectKey |
| 75 | + output, err := sample.obsClient.InitiateMultipartUpload(input) |
| 76 | + if err != nil { |
| 77 | + panic(err) |
| 78 | + } |
| 79 | + return output.UploadId |
| 80 | +} |
| 81 | + |
| 82 | +func (sample SimpleCustumHttpClientSample) UploadPart(uploadId string) (string, int) { |
| 83 | + input := &obs.UploadPartInput{} |
| 84 | + input.Bucket = sample.bucketName |
| 85 | + input.Key = sample.objectKey |
| 86 | + input.UploadId = uploadId |
| 87 | + input.PartNumber = 1 |
| 88 | + input.Body = strings.NewReader("Hello OBS") |
| 89 | + output, err := sample.obsClient.UploadPart(input) |
| 90 | + if err != nil { |
| 91 | + panic(err) |
| 92 | + } |
| 93 | + return output.ETag, output.PartNumber |
| 94 | +} |
| 95 | + |
| 96 | +func (sample SimpleCustumHttpClientSample) CompleteMultipartUpload(uploadId, etag string, partNumber int) { |
| 97 | + input := &obs.CompleteMultipartUploadInput{} |
| 98 | + input.Bucket = sample.bucketName |
| 99 | + input.Key = sample.objectKey |
| 100 | + input.UploadId = uploadId |
| 101 | + input.Parts = []obs.Part{ |
| 102 | + obs.Part{PartNumber: partNumber, ETag: etag}, |
| 103 | + } |
| 104 | + _, err := sample.obsClient.CompleteMultipartUpload(input) |
| 105 | + if err != nil { |
| 106 | + panic(err) |
| 107 | + } |
| 108 | + fmt.Printf("Upload object %s successfully!\n", sample.objectKey) |
| 109 | +} |
| 110 | + |
| 111 | +func RunCustumHttpClientSample() { |
| 112 | + const ( |
| 113 | + endpoint = "https://your-endpoint" |
| 114 | + ak = "*** Provide your Access Key ***" |
| 115 | + sk = "*** Provide your Secret Key ***" |
| 116 | + bucketName = "bucket-test" |
| 117 | + objectKey = "object-test" |
| 118 | + location = "yourbucketlocation" |
| 119 | + ) |
| 120 | + |
| 121 | + sample := newCustumHttpClientSample(ak, sk, endpoint, bucketName, objectKey, location) |
| 122 | + |
| 123 | + fmt.Println("Create a new bucket for demo") |
| 124 | + sample.CreateBucket() |
| 125 | + |
| 126 | + // Step 1: initiate multipart upload |
| 127 | + fmt.Println("Step 1: initiate multipart upload") |
| 128 | + uploadId := sample.InitiateMultipartUpload() |
| 129 | + |
| 130 | + // Step 2: upload a part |
| 131 | + fmt.Println("Step 2: upload a part") |
| 132 | + |
| 133 | + etag, partNumber := sample.UploadPart(uploadId) |
| 134 | + |
| 135 | + // Step 3: complete multipart upload |
| 136 | + fmt.Println("Step 3: complete multipart upload") |
| 137 | + sample.CompleteMultipartUpload(uploadId, etag, partNumber) |
| 138 | + |
| 139 | +} |
0 commit comments