Skip to content

Commit

Permalink
Add content type when uploading to s3
Browse files Browse the repository at this point in the history
  • Loading branch information
pratishshr committed Sep 3, 2019
1 parent 60cecbc commit a4d9f62
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ shift-infrastructure
# Configfiles
config.json

# artifacts
artifact
4 changes: 2 additions & 2 deletions cli/internals/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type deployment struct {
}

type projectRequest struct {
ProjectName string
Deployment deployment
ProjectName string `json:"projectName"`
Deployment deployment `json:"deployment"`
}

type buildInformation struct {
Expand Down
2 changes: 1 addition & 1 deletion cli/services/mq/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func failOnError(err error, msg string) {
}

func getQueue() (*amqp.Connection, *amqp.Channel, *amqp.Queue) {
conn, err := amqp.Dial("amqp://guest@localhost:5672")
conn, err := amqp.Dial("amqp://shift:shiftdeveloper@dev-shiftmq.lftechnology.com:5672")

failOnError(err, "Failed to connect to RabbitMQ")

Expand Down
1 change: 0 additions & 1 deletion deployment/artifact/lms-leave
Submodule lms-leave deleted from 27d5ca
13 changes: 10 additions & 3 deletions deployment/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ type deployment struct {
Bucket string `json:"bucket"`
}

type infrastructure struct {
BucketName string `json:"bucketName"`
URL string `json:"url"`
}

type projectResponse struct {
ProjectName string
Deployment deployment
ProjectName string `json:"projectName"`
Deployment deployment `json:"deployment"`
Data infrastructure
}

func deploy(msg []byte) {
Expand All @@ -44,7 +50,8 @@ func deploy(msg []byte) {
s3.Deploy(s3.Data{
AccessKey: project.Deployment.AccessKey,
SecretKey: project.Deployment.SecretKey,
Bucket: project.Deployment.Bucket,
Bucket: project.Data.BucketName,
URL: project.Data.URL,
DistFolder: project.Deployment.DistFolder,
})
}
Expand Down
2 changes: 2 additions & 0 deletions deployment/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.12
require (
github.com/aws/aws-sdk-go v1.23.13
github.com/briandowns/spinner v1.6.1
github.com/gabriel-vasile/mimetype v0.3.18
github.com/h2non/filetype v1.0.10
github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271
golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 // indirect
)
4 changes: 4 additions & 0 deletions deployment/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ github.com/briandowns/spinner v1.6.1 h1:LBxHu5WLyVuVEtTD72xegiC7QJGx598LBpo3ywKT
github.com/briandowns/spinner v1.6.1/go.mod h1://Zf9tMcxfRUA36V23M6YGEAv+kECGfvpnLTnb8n4XQ=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/gabriel-vasile/mimetype v0.3.18 h1:nSVMhs6i5wo99Ib8t5jCiA6QJV7VlHJh9C+sA8G5ytw=
github.com/gabriel-vasile/mimetype v0.3.18/go.mod h1:kMJbg3SlWZCsj4R73F1WDzbT9AyGCOVmUtIxxwO5pmI=
github.com/h2non/filetype v1.0.10 h1:z+SJfnL6thYJ9kAST+6nPRXp1lMxnOVbMZHNYHMar0s=
github.com/h2non/filetype v1.0.10/go.mod h1:isekKqOuhMj+s/7r3rIeTErIRy4Rub5uBWHfvMusLMU=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
Expand Down
15 changes: 10 additions & 5 deletions deployment/services/aws/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
awsService "github.com/leapfrogtechnology/shift/deployment/services/aws"
fileUtil "github.com/leapfrogtechnology/shift/deployment/utils/file"
"github.com/leapfrogtechnology/shift/deployment/utils/spinner"
)

Expand All @@ -18,6 +19,7 @@ type Data struct {
SecretKey string
Bucket string
DistFolder string
URL string
}

// Deploy to S3 bucket
Expand Down Expand Up @@ -47,23 +49,26 @@ func Deploy(data Data) {
for _, file := range fileList {
f, _ := os.Open(file)

key := strings.TrimPrefix(file, "artifact"+data.DistFolder)
key := strings.TrimPrefix(file, "artifact/"+data.DistFolder)
contentType := fileUtil.GetFileContentType(file)

// Upload the file to S3.
output, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(data.Bucket),
Key: aws.String(key),
Body: f,
Bucket: aws.String(data.Bucket),
Key: aws.String(key),
ContentType: aws.String(contentType),
Body: f,
})

if err != nil {
fmt.Println("error")
fmt.Println(err)
}

fmt.Println(output)
fmt.Println(output.Location)
}

fmt.Println("Uploaded all files.")
fmt.Println("Project deployed at " + data.URL)
spinner.Stop()
}
2 changes: 1 addition & 1 deletion deployment/services/mq/mq.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func failOnError(err error, msg string) {
}

func getQueue() (*amqp.Connection, *amqp.Channel, *amqp.Queue) {
conn, err := amqp.Dial("amqp://guest@localhost:5672")
conn, err := amqp.Dial("amqp://shift:shiftdeveloper@dev-shiftmq.lftechnology.com:5672")

failOnError(err, "Failed to connect to RabbitMQ")

Expand Down
19 changes: 19 additions & 0 deletions deployment/utils/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package file

import (
"mime"
"strings"
)

// GetFileContentType returns the content type of the file.
func GetFileContentType(filename string) string {
split := strings.Split(filename, ".")

if len(split) == 0 {
return "binary/octet-stream"
}

ext := split[len(split)-1]

return mime.TypeByExtension("." + ext)
}

0 comments on commit a4d9f62

Please sign in to comment.