Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could you provide a full S3 example #10

Open
christopheblin opened this issue Mar 23, 2018 · 0 comments
Open

Could you provide a full S3 example #10

christopheblin opened this issue Mar 23, 2018 · 0 comments

Comments

@christopheblin
Copy link

Hi,

I am trying to use your library to upload an object to S3 without the big fat aws-sdk-java jar

However, I dont get how you obtain the

String contentSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";

And I also dont get what is x-amz-date which is not described in https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html

Can you help ?

Here is how my code is looking (this is kotlin without any dependency but your lib)

class S3FileStorage(val accessKey: String, val secretKey: String, val bucket: String) : FileStorage {
    override fun upload(base64: String, mimeType:String): String {
        val bytes = Base64.getDecoder().decode(base64)
        val ext = mimeType.substring(mimeType.indexOf('/') + 1)
        val name = UUID.randomUUID().toString() + "." + ext

        val urlStr = "https://${bucket}.s3.amazonaws.com/${name}"
        val httpMethod = "PUT"
        val contentSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" //HOW TO GET THAT
        val request = HttpRequest(httpMethod, URI(urlStr))
        val signature = Signer.builder()
                .awsCredentials(AwsCredentials(accessKey, secretKey))
                .header("Host", "examplebucket.s3.amazonaws.com")
                .header("x-amz-date", "20130524T000000Z") //WHAT IS THIS ?
                .header("x-amz-content-sha256", contentSha256)
                .buildS3(request, contentSha256)
                .getSignature()

        val df = SimpleDateFormat("EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US)
        df.timeZone = TimeZone.getTimeZone("GMT")
        val formattedDate = df.format(Date())

        val url = URL(urlStr)
        val httpConn = url.openConnection() as HttpURLConnection
        httpConn.setDoOutput(true)
        httpConn.setRequestMethod(httpMethod)
        httpConn.setRequestProperty("Accept", "application/json")
        httpConn.setRequestProperty("Date", formattedDate)
        httpConn.setRequestProperty("Content-type", mimeType)
        httpConn.setRequestProperty("Authorization", signature)
        httpConn.getOutputStream().use { outputStream ->
            ByteArrayInputStream(bytes).use { byteArrayInputStream ->
                byteArrayInputStream.copyTo(outputStream)
            }
        }
        val res = httpConn.getResponseMessage()
        Logger.getLogger("BEASYNESS").info("UPLOAD TO S3 " + res)

        return urlStr
    }

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant