android - 如何在不使用 SDK 的情况下将文件从 Android 上传到 Amazon S3

标签 android amazon-web-services amazon-s3

我一直致力于使用亚马逊 S3 的 REST API 将文件从我的 Android 设备上传到我拥有的存储桶。我有 KEY 和 SECRET_KEY,但不确定如何正确生成他们在请求中寻找的签名值。我在他们的服务器上使用 HttpPut,但不确定如何正确生成 signatureValue。到目前为止,这是我所拥有的:

HttpPut put = new HttpPut(URL);

            String fmt = "EEE, dd MMM yyyy HH:mm:ss ";
            SimpleDateFormat format = new SimpleDateFormat(fmt, Locale.US);
            format.setTimeZone(TimeZone.getTimeZone("GMT"));

            String method = "PUT";
            String contentType = "application/octet-stream";
            String date = format.format(new Date()) + "GMT";
            String bucket = "/test-bucket52809/";

            StringBuffer buf = new StringBuffer();
            buf.append(method).append("\n\n");
            buf.append(contentType).append("\n");
            buf.append(date).append("\n");
            buf.append(bucket);

            String signature = percentEncodeRfc3986(hmac(buf.toString()));

然后这里是我用来生成签名值的方法:

    private void setupMac() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException
    {

        byte[] secretyKeyBytes = KEY_SECRET.getBytes("UTF-8");
        signingKey = new SecretKeySpec(secretyKeyBytes, "HmacSHA256");
        mac = Mac.getInstance("HmacSHA256");
        mac.init(signingKey);
    }

    private String hmac(String stringToSign) {
        String signature = null;
        byte[] data;
        byte[] rawHmac;
        try {
            data = stringToSign.getBytes("UTF-8");
            rawHmac = mac.doFinal(data);
            signature = new String(Base64.encode(rawHmac, Base64.DEFAULT));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UTF-8" + " is unsupported!", e);
        }
        return signature;
    }

    private String percentEncodeRfc3986(String s) {
        String out;
        try {
            out = URLEncoder.encode(s, "UTF-8").replace("+", "%20")
                    .replace("*", "%2A").replace("%7E", "~");
        } catch (UnsupportedEncodingException e) {
            out = s;
        }
        return out;
    }

我使用了 Amazon S3 签名测试器,我的字符串是正确的,但我从未得到正确的编码值。感谢您向正确方向提供的任何帮助或插入。

最佳答案

import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

String policy = (new BASE64Encoder()).encode(
    policy_document.getBytes("UTF-8")).replaceAll("\n","").replaceAll("\r","");

Mac hmac = Mac.getInstance("HmacSHA1");
hmac.init(new SecretKeySpec(
    aws_secret_key.getBytes("UTF-8"), "HmacSHA1"));
String signature = (new BASE64Encoder()).encode(
    hmac.doFinal(policy.getBytes("UTF-8")))
    .replaceAll("\n", "");

[引用:https://aws.amazon.com/articles/1434]

上面的链接也描述了HTTP请求中的输入参数。

关于android - 如何在不使用 SDK 的情况下将文件从 Android 上传到 Amazon S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19281215/

相关文章:

ruby-on-rails - 使用 aws-sdk for ruby​​ in rails 下载 S3 文件(对象)

java - Android Canvas 不绘图

scala - 如何覆盖 awsglue 中的数据?

python-3.x - 无法从版本控制的 s3 存储桶中删除项目

linux - Amazon Beanstalk 部署错误

amazon-web-services - Amazon Elastic Container Service 中的无法拉取容器错误

amazon-s3 - 如何修复 AWS S3 上损坏的 Delta Lake 表

android - 在android中将对象从一个应用程序传递到另一个应用程序

android - RecyclerView 在日志中显示空值但没有错误

android - Gradle - 从包含的库中覆盖依赖项(排除依赖项)