looking for the equivelant golang code in the aws s3 sdk, but have not found it yet. Anyone help here??? Thanks.

635 views
Skip to first unread message

JM

unread,
Jul 15, 2016, 2:31:01 PM7/15/16
to golang-nuts
I am trying to find the equivalent of getSignedURL and hoping i'm just blind and overlooking it somewhere.


//Generates a short lives signed url to be used from the client to upload an image.
function(req, res){
    aws.config.update({accessKeyId: AWS_ACCESS_KEY , secretAccessKey: AWS_SECRET_KEY });
    aws.config.update({region: process.env.REGION , signatureVersion: 'v4' });
    var s3 = new aws.S3();
    var s3_params = {
        Bucket: S3_BUCKET,
        Key: req.query.file_name,
        Expires: 60,
        ContentType: req.query.file_type,
        ACL: 'public-read'
    };
    s3.getSignedUrl('putObject', s3_params, function(err, data){
        if(err){
            console.log(err);
        } else {
            var return_data = {
                signed_request: data,
                url: 'https://'+S3_BUCKET+'.s3.amazonaws.com/'+req.query.file_name
            };
            res.write(JSON.stringify(return_data));
            res.end();
        }
    });



Peter Waller

unread,
Jul 18, 2016, 8:00:19 AM7/18/16
to JM, golang-nuts
Here's an example. It uses the machine's role credentials automatically. If you want to specify a secret manually, you'll have to read the documentation.


I can't guarantee it's uptodate, but something similar to this should work.

---

​// SignGetRequest returns a signed URL for downloading s3://{{bucket}}/{{key}}
// with a given time-to-live.
func SignGetRequest(bucket, key string, ttl time.Duration) (string, error) {
svc := s3.New(nil)
req, _ := svc.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key:    aws.String(key),
})
url, err := req.Presign(ttl)
if err != nil {
return "", &RequestError{path: path.Join(bucket, key), baseError: err}
}
return url, nil
}

Joshua Boelter

unread,
Jul 19, 2016, 7:27:19 PM7/19/16
to golang-nuts
Another example.
- ContentType below is optional, but would be part of the signed headers if you use it
- The req.HTTPRequest.Header.Set() allows you to add additional headers that will be part of the signed request


req, _ := s3Svc.PutObjectRequest(&s3.PutObjectInput{
Bucket:      aws.String(bucket),
Key:         aws.String(key),
Body:        nil,
ContentType: aws.String("application/octet-stream"),
})

req.HTTPRequest.Header.Set("Content-MD5", b64hash)
req.Operation.HTTPMethod = "PUT"

url, err := req.Presign(24 * time.Hour)
Reply all
Reply to author
Forward
0 new messages