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
item, err := dynamoDbGet(sess, key) | |
if err != nil { | |
log.Println(err) | |
return newFailedResponse(), nil | |
} | |
if item == nil { | |
// Key が見つからなかった場合は404 | |
return &Response{ | |
StatusCode: 404, | |
Body: `{"message": "not found"}`, | |
Headers: headerCORS, | |
}, nil | |
} | |
// DynamoDB からレスポンスを取り出して返却する | |
return &Response{ | |
StatusCode: item.Response.StatusCode, // 200/400/500 が返される | |
Body: item.Response.Body, | |
Headers: headerCORS, | |
}, nil |
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
func newItem(key string, code int, body string) *Item { | |
return &Item{ | |
ObjectKey: key, | |
TTL: (time.Now().AddDate(0, 0, 7)).Unix(), // 1週間後に消える | |
Response: &ResultResponse{ | |
StatusCode: code, | |
Body: body, | |
}, | |
} | |
} |
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
err := exec.Command("convert", trg, "-auto-orient", "+profile", "!icc,*", dst).Run() |
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
// クエリパラメータからtargetを取る | |
// targetは適当にバリデーションをしているが省略 | |
trg := req.QueryStringParameters["target"] | |
// Create S3 service client | |
svc := s3.New(sess) | |
u, err := uuid.NewRandom() // UUIDv4 | |
if err != nil { | |
return newFailedResponse(), nil | |
} | |
key := "press_kit/" + trg + "/" + u.String() | |
// 生成したオブジェクトキーに対するPUTアクセス用リクエスト | |
sreq, _ := svc.PutObjectRequest(&s3.PutObjectInput{ | |
Bucket: aws.String(os.Getenv("PRIVATE_BUCKET")), | |
Key: aws.String(key), | |
}) | |
// 15分間だけ有効な署名付きURLを生成する | |
urlStr, err := sreq.Presign(15 * time.Minute) | |
if err != nil { | |
return newFailedResponse(), nil | |
} |
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
func ConvertPressKitFile(ctx context.Context, event events.S3Event) { | |
// 引数はRequestではない事に注意 | |
for _, record := range event.Records { | |
bucket := record.S3.Bucket.Name | |
key := record.S3.Object.Key | |
// S3のPUTをトリガーにするので、ObjectKeyからTargetを取得する | |
trg, err := getTargetFromKey(key) | |
// 非公開バケットからオブジェクトをダウンロード | |
tmpFile, err := os.CreateTemp("/tmp", "srctmp_") | |
downloader := s3manager.NewDownloader(sess) | |
_, err = downloader.Download( | |
tmpFile., | |
&s3.GetObjectInput{ | |
Bucket: aws.String(bucket), | |
Key: aws.String(key), | |
}) | |
// ファイルの情報を取得 | |
fi, err := converter.MakeFileInfo(tmpFile) | |
// 取得したファイル情報でバリデーション | |
verrs := validator.ValidateFileInfo(fi, trg) | |
if len(verrs) > 0 { | |
// バリデーションエラーのリストからレスポンスを生成してDynamoDBへPUT | |
err = dynamoDbPut(sess, newValidationErrorItem(key, verrs)) | |
continue | |
} | |
// 画像の変換処理(JPEGだけ) | |
if fi.ContntType == converter.JPEGMIMEType { | |
dstFile, err := os.CreateTemp("/tmp", "dsttmp_") | |
err = converter.ExecStripExif(tmpFile.Name(), dstFile.Name()) | |
} | |
// 公開用のS3バケットへアップロード | |
result, err := s3Upload(sess, fi.TmpName, createPublicS3Key(key, fi.Extension), fi.ContntType) | |
// DynamoDB へアップロード | |
// 取得したファイル情報(+オブジェクトキー)からレスポンスを生成してPUT | |
bytes, err := json.Marshal(fi) | |
err = dynamoDbPut(sess, newItem(key, 200, string(bytes))) | |
} | |
} |
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
link: |
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
result, err := uploader.Upload(&s3manager.UploadInput{ | |
Bucket: aws.String(os.Getenv("PUBLIC_BUCKET")), | |
Key: aws.String(key), | |
Body: f, | |
CacheControl: aws.String("public, max-age=31536000"), // 1 year | |
ContentType: aws.String(ctype), | |
Metadata: aws.StringMap(amzMeta), // 先程作ったmap | |
}) |
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
amzMeta := make(map[string]string) | |
amzMeta["surrogate-key"] = strings.Replace(key, "/", " ", -1) // 値はS3オブジェクトキーのプレフィックスをスペース区切りにした |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment