2

References:

By "happy" names, I mean the real name of the file I'm uploading... for instance, if I'm putting a file called "foo.png" I'd expect the url to the file to be /foo.png. Currently, I'm just getting what appears to be a GUID (with no file extension) for the file name.

Any ideas?

longda
  • 10,153
  • 7
  • 46
  • 66

3 Answers3

2

With length, inputstream and fileName given from the uploaded file, you should achieve what you want with the following code :

        S3Service s3Service = new RestS3Service(new AWSCredentials(accessKey, secretKey))
        S3Object up = new S3Object(s3Service.getBucket("myBucketName"), fileName)
        up.setAcl AccessControlList.REST_CANNED_PUBLIC_READ
        up.setContentLength length
        up.setContentType "image/jpeg"
        up.setDataInputStream inputstream
        up = s3Service.putObject(bucket, up)

I hope it helps.

fabien7474
  • 16,300
  • 22
  • 96
  • 124
2

You can set the key field on the S3Asset object to achieve what you need.

I'll update the doco page with more information on this.

leebutts
  • 4,882
  • 1
  • 23
  • 25
1

Actual solution (as provided by @leebutts):

import java.io.*;
import org.grails.s3.*;

def s3AssetService;
def file = new File("foo.png"); //assuming this file exists     
def asset = new S3Asset(file);
asset.mimeType = extension;
asset.key = "foo.png"
s3AssetService.put(asset);
longda
  • 10,153
  • 7
  • 46
  • 66