如何将图像从js客户端保存到blobstore?

时间:2015-11-13 14:33:32

标签: javascript google-app-engine google-cloud-endpoints blobstore

我想将浏览器中的图像保存到google blobstore中。 我已经尝试过的: 将我的img转换为base64字符串:

reader = new FileReader();
reader.readAsBinaryString(file);

调用我的端点功能将其发送到谷歌应用引擎。

var request = gapi.client.helloworldendpoints.uploadImage({'imageData': __upload.imageData, 'fileName': __upload.fileName, 'mimeType': __upload.mimeType, 'size': __upload.size});
    request.execute(
            function (result) {
                console.log("Callback:");
                console.log(result);
            }
    );

我在Java中的EndPoint看起来像这样:

 @ApiMethod(name = "uploadImage", path = "/uploadImage", httpMethod = "POST")
public ImageUploadRequest uploadImage(@Named("imageData") byte[] imageData, @Named("fileName") String fileName, @Named("mimeType") String mimeType, @Named("size") float size) { 

return new ImageUploadRequest(imageData, fileName, mimeType, size);
}

问题是,我的端点似乎无法处理我的base64的传输。我总是得到503后端错误

通过app引擎将数据从我的js客户端发送到blobstore的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

UPDATE 主要问题解决了。我设法上传了blob。

Javascript

<?php
// signature, publicKeyUrl, timestamp and salt are included in the base64/json data you will receive by calling generateIdentityVerificationSignatureWithCompletionHandler.

$timestamp = $params["timestamp"]; // e.g. 1447754520194
$user_id = $params["user_id"]; // e.g. G:20010412315
$bundle_id = "com.example.test";
$public_key_url = $params["publicKeyUrl"]; // e.g. https://static.gc.apple.com/public-key/gc-prod-2.cer
$salt = base64_decode($params["salt"]); // Binary
$signature = base64_decode($params["signature"]); // Binary

// Timestamp is unsigned 64-bit integer big endian
$highMap = 0xffffffff00000000;
$lowMap = 0x00000000ffffffff;
$higher = ($timestamp & $highMap) >>32;
$lower = $timestamp & $lowMap;
$timestamp = pack('NN', $higher, $lower);

// Concatenate the string
$data = $user_id . $bundle_id . $timestamp . $salt;

// ATTENTION!!! Do not hash it! $data = hash("sha256", $packed);

// Fetch the certificate. This is dirty because it is neither cached nor verified that the url belongs to Apple.
$ssl_certificate = file_get_contents($public_key_url);

$pem = chunk_split(base64_encode($ssl_certificate), 64, "\n");
$pem = "-----BEGIN CERTIFICATE-----\n" . $pem . "-----END CERTIFICATE-----\n";

// it is also possible to pass the $pem string directly to openssl_verify
if (($pubkey_id = openssl_pkey_get_public($pem)) === false) {
    echo "invalid public key\n";
    exit;
}

// Verify that the signature is correct for $data
$verify_result = openssl_verify($data, $signature, $pubkey_id, OPENSSL_ALGO_SHA256);

openssl_free_key($pubkey_id);

switch($verify_result) {
  case 1:
    echo "Signature is ok.\n";
    break;
  case 0:
    echo "Signature is wrong.\n";
    break;
  default:
    echo "An error occurred.\n";
    break;
}

Java端点

var request = gapi.client.helloworldendpoints.uploadImage({
    'imageData': __upload.imageData, 
    'fileName': __upload.fileName, 
    'mimeType': __upload.mimeType, 
    'size': __upload.size
});

请求只是这个

public ImageUploadRequest uploadImage(
    Request imageData, 
    @Named("fileName") String fileName, 
    @Named("mimeType") String mimeType, 
    @Named("size") float size
) { ... }

我的下一个问题如下 如何从GAE的Java端点向我的UploadServlet发送MultipartRequest以创建blobkey并将数据保存到blobstorage中,因为Blobstorage只接受发送到servlet的数据?