|
| 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 | + "os" |
| 19 | + "time" |
| 20 | + "samples/utils" |
| 21 | +) |
| 22 | + |
| 23 | +func main() { |
| 24 | + // Create a client from environment variables. |
| 25 | + client, err := buildclient.FromEnv() |
| 26 | + if err != nil { |
| 27 | + log.Fatal(err) |
| 28 | + } |
| 29 | + |
| 30 | + // Create a bucket where our files will be stored. |
| 31 | + _, err = client.PutBucket(models.NewPutBucketRequest(utils.BucketName)) |
| 32 | + if err != nil { |
| 33 | + log.Fatal(err) |
| 34 | + } |
| 35 | + |
| 36 | + // Create the list of Ds3PutObjects to be put to the Black Pearl via the bulk put |
| 37 | + // command. For each object we need its name and size. |
| 38 | + var ds3PutObjects []models.Ds3PutObject |
| 39 | + for _, book := range utils.BookNames { |
| 40 | + fi, err := os.Stat(utils.ResourceFolder + book) |
| 41 | + if err != nil { |
| 42 | + log.Fatal(err) |
| 43 | + } |
| 44 | + curObj := models.Ds3PutObject{ Name:book, Size:fi.Size()} |
| 45 | + ds3PutObjects = append(ds3PutObjects, curObj) |
| 46 | + } |
| 47 | + |
| 48 | + // Create the bulk put request. |
| 49 | + putBulkRequest := models.NewPutBulkJobSpectraS3Request(utils.BucketName, ds3PutObjects) |
| 50 | + |
| 51 | + // Send the bulk put request to the server. This creates the job, but does not |
| 52 | + // directly send the data. |
| 53 | + putBulkResponse, err := client.PutBulkJobSpectraS3(putBulkRequest) |
| 54 | + if err != nil { |
| 55 | + log.Fatal(err) |
| 56 | + } |
| 57 | + |
| 58 | + // The bulk request will split the files over several chunks if it needs to. |
| 59 | + // We then need to ask what chunks we can send, and then send them making sure |
| 60 | + // we don't resend the same chunks. |
| 61 | + |
| 62 | + // The bulk job is split into multiple chunks, which then need to be transferred. |
| 63 | + totalChunkCount := len(putBulkResponse.MasterObjectList.Objects) |
| 64 | + curChunkCount := 0 |
| 65 | + |
| 66 | + for curChunkCount < totalChunkCount { |
| 67 | + // Get the list of available chunks that the server can receive. The server may |
| 68 | + // not be able to receive everything, so not all chunks will necessarily be |
| 69 | + // returned |
| 70 | + chunksReady := models.NewGetJobChunksReadyForClientProcessingSpectraS3Request(putBulkResponse.MasterObjectList.JobId) |
| 71 | + chunksReadyResponse, err := client.GetJobChunksReadyForClientProcessingSpectraS3(chunksReady) |
| 72 | + if err != nil { |
| 73 | + log.Fatal(err) |
| 74 | + } |
| 75 | + |
| 76 | + // Check to see if any chunks can be processed |
| 77 | + numberOfChunks := len(chunksReadyResponse.MasterObjectList.Objects) |
| 78 | + if numberOfChunks > 0 { |
| 79 | + // Loop through all the chunks that are available for processing, and send |
| 80 | + // the files that are contained within them. |
| 81 | + for _, curChunk := range chunksReadyResponse.MasterObjectList.Objects { |
| 82 | + |
| 83 | + for _, curObj := range curChunk.Objects { |
| 84 | + |
| 85 | + reader, err := utils.LoadBook(*curObj.Name) |
| 86 | + if err != nil { |
| 87 | + log.Fatal(err) |
| 88 | + } |
| 89 | + |
| 90 | + putObjRequest := models.NewPutObjectRequest(utils.BucketName, *curObj.Name, *reader). |
| 91 | + WithJob(chunksReadyResponse.MasterObjectList.JobId). |
| 92 | + WithOffset(curObj.Offset) |
| 93 | + |
| 94 | + _, err = client.PutObject(putObjRequest) |
| 95 | + if err != nil { |
| 96 | + log.Fatal(err) |
| 97 | + } |
| 98 | + } |
| 99 | + curChunkCount++ |
| 100 | + } |
| 101 | + } else { |
| 102 | + // When no chunks are returned we need to sleep to allow for cache space to |
| 103 | + // be freed. |
| 104 | + time.Sleep(time.Second * 5) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments