github.com/coreos/mantle@v0.13.0/cmd/ore/azure/upload-blob.go (about) 1 // Copyright 2016 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package azure 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/Microsoft/azure-vhd-utils/vhdcore/validator" 22 "github.com/spf13/cobra" 23 ) 24 25 var ( 26 cmdUploadBlob = &cobra.Command{ 27 Use: "upload-blob storage-account container blob-name file", 28 Short: "Upload a blob to Azure storage", 29 Run: runUploadBlob, 30 } 31 32 // upload blob options 33 ubo struct { 34 storageacct string 35 container string 36 blob string 37 vhd string 38 overwrite bool 39 validate bool 40 } 41 ) 42 43 func init() { 44 bv := cmdUploadBlob.Flags().BoolVar 45 46 bv(&ubo.overwrite, "overwrite", false, "overwrite blob") 47 bv(&ubo.validate, "validate", true, "validate blob as VHD file") 48 49 Azure.AddCommand(cmdUploadBlob) 50 } 51 52 func runUploadBlob(cmd *cobra.Command, args []string) { 53 if len(args) != 4 { 54 plog.Fatalf("Expecting 4 arguments, got %d", len(args)) 55 } 56 57 ubo.storageacct = args[0] 58 ubo.container = args[1] 59 ubo.blob = args[2] 60 ubo.vhd = args[3] 61 62 if ubo.validate { 63 plog.Printf("Validating VHD %q", ubo.vhd) 64 if !strings.HasSuffix(strings.ToLower(ubo.blob), ".vhd") { 65 plog.Fatalf("Blob name should end with .vhd") 66 } 67 68 if err := validator.ValidateVhd(ubo.vhd); err != nil { 69 plog.Fatal(err) 70 } 71 72 if err := validator.ValidateVhdSize(ubo.vhd); err != nil { 73 plog.Fatal(err) 74 } 75 } 76 77 kr, err := api.GetStorageServiceKeys(ubo.storageacct) 78 if err != nil { 79 plog.Fatalf("Fetching storage service keys failed: %v", err) 80 } 81 82 if err := api.UploadBlob(ubo.storageacct, kr.PrimaryKey, ubo.vhd, ubo.container, ubo.blob, ubo.overwrite); err != nil { 83 plog.Fatalf("Uploading blob failed: %v", err) 84 } 85 86 uri := fmt.Sprintf("https://%s.blob.core.windows.net/%s/%s", ubo.storageacct, ubo.container, ubo.blob) 87 88 plog.Printf("Blob uploaded to %q", uri) 89 }