-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from traPtitech/feat/s3
- Loading branch information
Showing
6 changed files
with
180 additions
and
76 deletions.
There are no files selected for viewing
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,83 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
"github.com/aws/aws-sdk-go-v2/service/s3" | ||
) | ||
|
||
type S3Storage struct { | ||
bucket string | ||
client *s3.Client | ||
} | ||
|
||
// S3 (互換) オブジェクトストレージをカレントストレージに設定する | ||
func SetS3Storage(bucket, region, endpoint, apiKey, apiSecret string) error { | ||
cfg, err := config.LoadDefaultConfig(context.Background(), | ||
config.WithRegion(region), | ||
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(apiKey, apiSecret, "")), | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to load configuration: %w", err) | ||
} | ||
|
||
client := s3.NewFromConfig(cfg, func(o *s3.Options) { | ||
o.UsePathStyle = true | ||
o.BaseEndpoint = &endpoint | ||
}) | ||
|
||
current = &S3Storage{ | ||
bucket: bucket, | ||
client: client, | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *S3Storage) Save(filename string, src io.Reader) error { | ||
input := &s3.PutObjectInput{ | ||
Bucket: aws.String(s.bucket), | ||
Key: aws.String(filename), | ||
Body: src, | ||
} | ||
|
||
_, err := s.client.PutObject(context.Background(), input) | ||
if err != nil { | ||
return fmt.Errorf("failed to put object: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *S3Storage) Open(filename string) (io.ReadCloser, error) { | ||
input := &s3.GetObjectInput{ | ||
Bucket: aws.String(s.bucket), | ||
Key: aws.String(filename), | ||
} | ||
|
||
output, err := s.client.GetObject(context.Background(), input) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get object: %w", err) | ||
} | ||
|
||
return output.Body, nil | ||
} | ||
|
||
func (s *S3Storage) Delete(filename string) error { | ||
input := &s3.DeleteObjectInput{ | ||
Bucket: aws.String(s.bucket), | ||
Key: aws.String(filename), | ||
} | ||
|
||
_, err := s.client.DeleteObject(context.Background(), input) | ||
if err != nil { | ||
return fmt.Errorf("failed to delete object: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.