Google云端存储 - 表单上传(POST OBJECT),签名和策略问题

时间:2014-01-08 11:20:15

标签: java google-app-engine gwt signature google-cloud-storage

尝试使用Google App Engine(GAE),GWT上托管的网页上的表单,使用POST OBJECT和政策文档,将文件上传到Google云端存储(GCS)时遇到了巨大问题和签名。

最大的问题;我没有得到任何关于错误的反馈。

首先。如果我根本不使用Policy或Signature,我可以将文件上传到GCS,所以我假设错误在于该部分。请参阅下面的代码。

//Is used to get the policy as base64 and the signature (and converted to base64)
public String[] getPolicyAndSignature(String policy)
{
    try
    {
       KeyStore keystore = KeyStore.getInstance("PKCS12");
       keystore.load(new FileInputStream(new File("my_test_app/mykey.p12")), "notasecret".toCharArray());
       PrivateKey privateKey = (PrivateKey)keystore.getKey("privatekey", "notasecret".toCharArray());

       Signature signature = Signature.getInstance("SHA256withRSA");
       signature.initSign(privateKey);

       //is this wrong, should it be just: (?)
       //signature.update(policy.getBytes())
       signature.update(Base64Utils.toBase64(policy.getBytes()).getBytes());

       byte[] signatureBytes = signature.sign();
       byte[] policyBytes = policy.getBytes();

       return new String[]{Base64Utils.toBase64(policyBytes), Base64Utils.toBase64(signatureBytes)};
    }
    catch(Exception e)
    {
       System.out.println("Error: " + e.getMessage());
    }
    return null;
}

这是使用该功能的代码:

public TestUploader()
{
    VerticalPanel mainPanel = new VerticalPanel();
    final FormPanel formPanel = new FormPanel();
    formPanel.setWidget(mainPanel);

    FileUpload upload = new FileUpload();
    upload.setName("file");
    formPanel.setAction("http://sgacms.storage.googleapis.com");
    formPanel.setMethod(FormPanel.METHOD_POST);
    formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
    formPanel.getElement().setAttribute("accept-charset", "UTF-8");

    String keyString = "testing";
    String bucketString = "sgacms";
    Hidden key = new Hidden("key", keyString);
    Hidden bucket = new Hidden("bucket", bucketString);
    Hidden contentType = new Hidden("Content-Type", "image/jpeg");
    Hidden GoogleAccessID = new Hidden("GoogleAccessID", "517031602192-p8kn8m33vhmhg9horbif8mansmkilqle@developer.gserviceaccount.com");
    Hidden acl = new Hidden("acl", "bucket-owner-read");
    final Hidden policy = new Hidden("policy");
    final Hidden hiddenSignature = new Hidden("signature");

    String policyString =
        "{\"expiration\":\"2016-06-16T11:11:11Z\","+
            "\"conditions\":["+
                "[\"starts-with\",\"$key\",\"\"],"+
                "{\"acl\":\"bucket-owner-read\"},"+
                "{\"bucket\":\"sgacms\"},"+
                "[\"eq\",\"$Content-Type\",\"image/jpeg\"],"+
                "[\"content-length-range\",0,1000000]"+
            "]"+
        "}";

    String[] policyAndSignature = getPolicyAndSignature(policyString);
    policy.setValue(policyAndSignature[0]);
    hiddenSignature.setValue(policyAndSignature[1]);

    Button btn = new Button("Submit");

    mainPanel.add(btn); //submit
    mainPanel.add(key); //hidden
    mainPanel.add(bucket); //hidden
    mainPanel.add(contentType); //hidden
    mainPanel.add(GoogleAccessID); //hidden
    mainPanel.add(acl); //hidden
    mainPanel.add(policy);  //hidden
    mainPanel.add(hiddenSignature); //hidden
    mainPanel.add(upload); //file

    initWidget(formPanel);

    btn.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            formPanel.submit();
        }
    });
    formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
        @Override
        public void onSubmitComplete(SubmitCompleteEvent event) {
            log.severe("Submit results: " + event.getResults());
        }
    });
}

提交结果只会打印:null,当我失败时。由于我从未成功过,因此从未打印任何其他内容。另一方面,如果我不使用策略而没有签名(换句话说是匿名上传),则提交结果将为空,文件将成功上传。

所以我无法弄清楚出了什么问题!任何帮助表示赞赏!谢谢!

编辑: 所以现在我终于得到了一个错误响应,说明如下: “SignatureDoesNotMatch:我们计算的请求签名与您提供的签名不符。请检查您的Google密钥和签名方法”

1 个答案:

答案 0 :(得分:2)

好的,所以我找到了问题的答案。

我有点生气,这是一个“简单”修复。正如您在我的问题中所看到的,我正在使用Base64Utils,它是“com.google.gwt.user.server.Base64Utils”包的一部分。我认为这是错误的,无论是我错误地使用它还是使用了一些默认的字符编码并忽略了它收到的数据的编码,我根本就不知道。我把它转出来使用“org.apache.commons.codec.binary.Base64”软件包中的Base64,它似乎有效。

(值得一提的是我确实尝试将Base64Utils与使用getBytes(“UTF-8”)结合使用 - 但这也不起作用。)

所以工作方法如下:

public String[] getPolicyAndSignature(String policy)
{
    try
    {
        String encodedPolicy = new String(Base64.encodeBase64(policy.getBytes()));

        KeyStore keystore = KeyStore.getInstance("PKCS12");
        keystore.load(new FileInputStream(new File("my_test_app/mykey.p12")), "notasecret".toCharArray());
        PrivateKey privateKey = (PrivateKey)keystore.getKey("privatekey", "notasecret".toCharArray());

        Signature signature = Signature.getInstance("SHA256withRSA");
        signature.initSign(privateKey);
        signature.update(encodedPolicy.getBytes());

        String encodedSignature = new String(Base64.encodeBase64(signature.sign(), false));

        return new String[]{encodedPolicy, encodedSignature};
    }
    catch(Exception e)
    {
        System.out.println("Error: " + e.getMessage());
    }
    return null;
}