github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/blob_store/s3_blob_store/blob_store.go (about) 1 package s3_blob_store 2 3 import ( 4 "io" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/aws/credentials" 8 "github.com/aws/aws-sdk-go/aws/session" 9 "github.com/aws/aws-sdk-go/service/s3" 10 11 "github.com/cloudfoundry-incubator/bbs/models" 12 "github.com/cloudfoundry-incubator/ltc/blob_store/blob" 13 config_package "github.com/cloudfoundry-incubator/ltc/config" 14 ) 15 16 type BlobStore struct { 17 Bucket string 18 S3 *s3.S3 19 blobTarget config_package.S3BlobStoreConfig 20 } 21 22 func New(blobTarget config_package.S3BlobStoreConfig) *BlobStore { 23 client := s3.New(session.New(&aws.Config{ 24 Credentials: credentials.NewStaticCredentials(blobTarget.AccessKey, blobTarget.SecretKey, ""), 25 Region: aws.String(blobTarget.Region), 26 S3ForcePathStyle: aws.Bool(true), 27 })) 28 29 return &BlobStore{ 30 Bucket: blobTarget.BucketName, 31 S3: client, 32 blobTarget: blobTarget, 33 } 34 } 35 36 func (b *BlobStore) List() ([]blob.Blob, error) { 37 objects, err := b.S3.ListObjects(&s3.ListObjectsInput{ 38 Bucket: aws.String(b.Bucket), 39 }) 40 if err != nil { 41 return nil, err 42 } 43 44 blobs := []blob.Blob{} 45 for _, obj := range objects.Contents { 46 blobs = append(blobs, blob.Blob{ 47 Path: *obj.Key, 48 Size: *obj.Size, 49 Created: *obj.LastModified, 50 }) 51 } 52 53 return blobs, nil 54 } 55 56 func (b *BlobStore) Upload(path string, contents io.ReadSeeker) error { 57 _, err := b.S3.PutObject(&s3.PutObjectInput{ 58 Bucket: aws.String(b.Bucket), 59 ACL: aws.String("private"), 60 Key: aws.String(path), 61 Body: contents, 62 }) 63 return err 64 } 65 66 func (b *BlobStore) Download(path string) (io.ReadCloser, error) { 67 output, err := b.S3.GetObject(&s3.GetObjectInput{ 68 Bucket: aws.String(b.Bucket), 69 Key: aws.String(path), 70 }) 71 return output.Body, err 72 } 73 74 func (b *BlobStore) Delete(path string) error { 75 _, err := b.S3.DeleteObject(&s3.DeleteObjectInput{ 76 Bucket: aws.String(b.Bucket), 77 Key: aws.String(path), 78 }) 79 return err 80 } 81 82 func (b *BlobStore) DownloadAppBitsAction(dropletName string) *models.Action { 83 return models.WrapAction(&models.SerialAction{ 84 LogSource: "DROPLET", 85 Actions: []*models.Action{ 86 models.WrapAction(&models.RunAction{ 87 Path: "/tmp/s3tool", 88 Dir: "/", 89 Args: []string{ 90 "get", 91 b.blobTarget.AccessKey, 92 b.blobTarget.SecretKey, 93 b.Bucket, 94 b.blobTarget.Region, 95 "/" + dropletName + "-bits.zip", 96 "/tmp/bits.zip", 97 }, 98 User: "vcap", 99 }), 100 models.WrapAction(&models.RunAction{ 101 Path: "/bin/mkdir", 102 Args: []string{"/tmp/app"}, 103 User: "vcap", 104 }), 105 models.WrapAction(&models.RunAction{ 106 Path: "/usr/bin/unzip", 107 Dir: "/tmp/app", 108 Args: []string{"-q", "/tmp/bits.zip"}, 109 User: "vcap", 110 }), 111 }, 112 }) 113 } 114 115 func (b *BlobStore) DeleteAppBitsAction(dropletName string) *models.Action { 116 return models.WrapAction(&models.RunAction{ 117 Path: "/tmp/s3tool", 118 Dir: "/", 119 Args: []string{ 120 "delete", 121 b.blobTarget.AccessKey, 122 b.blobTarget.SecretKey, 123 b.Bucket, 124 b.blobTarget.Region, 125 "/" + dropletName + "-bits.zip", 126 }, 127 User: "vcap", 128 LogSource: "DROPLET", 129 }) 130 } 131 132 func (b *BlobStore) UploadDropletAction(dropletName string) *models.Action { 133 return models.WrapAction(&models.RunAction{ 134 Path: "/tmp/s3tool", 135 Dir: "/", 136 Args: []string{ 137 "put", 138 b.blobTarget.AccessKey, 139 b.blobTarget.SecretKey, 140 b.Bucket, 141 b.blobTarget.Region, 142 "/" + dropletName + "-droplet.tgz", 143 "/tmp/droplet", 144 }, 145 User: "vcap", 146 LogSource: "DROPLET", 147 }) 148 } 149 150 func (b *BlobStore) DownloadDropletAction(dropletName string) *models.Action { 151 return models.WrapAction(&models.SerialAction{ 152 LogSource: "DROPLET", 153 Actions: []*models.Action{ 154 models.WrapAction(&models.RunAction{ 155 Path: "/tmp/s3tool", 156 Dir: "/", 157 Args: []string{ 158 "get", 159 b.blobTarget.AccessKey, 160 b.blobTarget.SecretKey, 161 b.Bucket, 162 b.blobTarget.Region, 163 "/" + dropletName + "-droplet.tgz", 164 "/tmp/droplet.tgz", 165 }, 166 User: "vcap", 167 }), 168 models.WrapAction(&models.RunAction{ 169 Path: "/bin/tar", 170 Args: []string{"zxf", "/tmp/droplet.tgz"}, 171 Dir: "/home/vcap", 172 User: "vcap", 173 }), 174 }, 175 }) 176 }