Skip to content

Commit

Permalink
add headfile helper for s3 to verify object
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Nov 22, 2023
1 parent 5173ef6 commit 30709bd
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions zipserver/s3_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package zipserver

import (
"context"
"encoding/json"
"io"
"log"
"net/url"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
Expand Down Expand Up @@ -55,3 +59,37 @@ func (c *S3Storage) PutFile(ctx context.Context, bucket, key string, contents io

return nil
}

// get some specific metadata for file
func (c *S3Storage) HeadFile(ctx context.Context, bucket, key string) (url.Values, error) {
svc := s3.New(c.Session)
input := &s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}

result, err := svc.HeadObjectWithContext(ctx, input)
if err != nil {
return nil, err
}

headJson, err := json.Marshal(result)
if err == nil {
log.Print("Head", string(headJson))
}

out := url.Values{}
if result.ChecksumSHA256 != nil {
out.Add("ChecksumSHA256", *result.ChecksumSHA256)
}

if result.ContentType != nil {
out.Add("ContentType", *result.ContentType)
}

if result.ContentLength != nil {
out.Add("ContentLength", strconv.FormatInt(*result.ContentLength, 10))
}

return out, nil
}

0 comments on commit 30709bd

Please sign in to comment.