Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add uploader to save to external storage
- Loading branch information
1 parent
123ebd8
commit 646a6c3
Showing
7 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package s3 | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/YuheiNakasaka/radiorec/config" | ||
"github.com/YuheiNakasaka/radiorec/internal/uploader" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awsutil" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
) | ||
|
||
// AwsS3 is struct to upload to aws s3, which fill uploader interface. | ||
type AwsS3 struct { | ||
uploader.Uploader | ||
} | ||
|
||
// Upload : upload to s3 | ||
func (s *AwsS3) Upload(path string, filename string) error { | ||
fmt.Println("Uploading...") | ||
|
||
// Read config file | ||
myconf := config.Config{} | ||
err := myconf.Init() | ||
if err != nil { | ||
return fmt.Errorf("Failed to load config %v", err) | ||
} | ||
|
||
accessKeyID := fmt.Sprintf("%v", myconf.List.Get("aws.s3.access_key_id")) | ||
secretAccessKey := fmt.Sprintf("%v", myconf.List.Get("aws.s3.secret_access_key")) | ||
region := fmt.Sprintf("%v", myconf.List.Get("aws.s3.region")) | ||
bucketName := fmt.Sprintf("%v", myconf.List.Get("aws.s3.bucket")) | ||
|
||
file, err := os.Open(path) | ||
if err != nil { | ||
return fmt.Errorf("Failed to open file: %v", err) | ||
} | ||
defer file.Close() | ||
|
||
cli := s3.New(session.New(), &aws.Config{ | ||
Credentials: credentials.NewStaticCredentials(accessKeyID, secretAccessKey, ""), | ||
Region: aws.String(region), | ||
}) | ||
|
||
resp, err := cli.PutObject(&s3.PutObjectInput{ | ||
Bucket: aws.String(bucketName), | ||
Key: aws.String(filename), | ||
ACL: aws.String("public-read"), | ||
ContentType: aws.String("video/mp4"), | ||
Body: file, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Failed to upload: %v", err) | ||
} | ||
|
||
log.Println(awsutil.StringValue(resp)) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package uploader | ||
|
||
// Uploader is interface of uploading tasks | ||
type Uploader interface { | ||
Upload(string, string) error | ||
} |