如何添加NodeJs,S3,heroku直接上传到android?

时间:2015-05-23 10:38:56

标签: android node.js heroku amazon-s3

我试图理解如何将他们提供的示例从网站转换为android / retrofit ......

这是该网站的代码示例:

function get_signed_request(file){
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "/sign_s3?file_name="+file.name+"&file_type="+file.type);
    xhr.onreadystatechange = function(){
        if(xhr.readyState === 4){
            if(xhr.status === 200){
                var response = JSON.parse(xhr.responseText);
                upload_file(file, response.signed_request, response.url);
            }
            else{
                alert("Could not get signed URL.");
            }
        }
    };
    xhr.send();
}


function upload_file(file, signed_request, url){
    var xhr = new XMLHttpRequest();
    xhr.open("PUT", signed_request);
    xhr.setRequestHeader('x-amz-acl', 'public-read');
    xhr.onload = function() {
        if (xhr.status === 200) {
            document.getElementById("preview").src = url;
            document.getElementById("avatar_url").value = url;
        }
    };
    xhr.onerror = function() {
        alert("Could not upload file.");
    };
    xhr.send(file);
}

这是我的机器人改装尝试:

@GET("/sign_s3") 
        public void getSign(
                @Query("name") String userId,
                @Query("type") String type,
                Callback<UserResponse> callback);

        @Multipart
        @PUT("/{url}") 
        public void sendMedia(
                @Path("url") String signRequest,
                @Part("theNameToUse") String theNameToUse,
                @Part("isItAPicture") boolean isItAPicture, //if true it is a picture
                @Part("media") TypedFile media,
                Callback<UserResponse> callback);

用户响应:

ApiManager.getAsyncApi().getSign(name, type, new Callback<UserResponse>() {
            @Override
            public void success(UserResponse userResponse, Response response) {
sendMedia(response.signed_request);
            }

            @Override
            public void failure(RetrofitError error) {
throw error;
            }
        });

private sendApi(path) {


 ApiManager.getAsyncApi().sendMedia(path, title, isPictureNotvideo, media, new Callback<UserResponse>() {
            @Override
            public void success(UserResponse userResponse, Response response) {

            }

            @Override
            public void failure(RetrofitError error) {
throw error;
            }
        }); 
}

heroku文档的链接是:https://devcenter.heroku.com/articles/s3-upload-node

response.signed_request不起作用,不是一个选项...如果我做response.getBody()这是我唯一可以看到或获取标题....不知道如何获取签名的请求...

0 个答案:

没有答案