微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用从服务器端应用程序生成的签名URL将文件(视频文件)从android上传到S3服务器

我正在尝试使用从服务器端(在python中编码)应用程序生成的签名URL将视频文件Android应用程序上传到S3服务器中的Bucket.我们正在向签名的URL发出PUT请求但我们正在收到

connection reset by peer exception.

但是当我在POSTMAN REST CLIENT上尝试相同的URL时,会获得成功消息.
任何帮助将不胜感激.

解决方法:

使用Retrofit HTTP客户端库完成此操作,它成功将文件上传到Amazon s3服务器.

码:

 public interface UploadService {
    String BASE_URL = "https://bucket.s3.amazonaws.com/folder";

    /**
     * @param url :signed s3 url string after 'BASE_URL'.
     * @param file :file to upload,( usage: new TypedFile("mp4", videoFile);.
     * @param cb :callback.
     */
    @PUT("/{url}")
    void uploadFile(@Path(value = "url", encode=false) String url, @Body() TypedFile file, Callback<String> cb);
}

服务类

public final class ServiceGenerator {

private ServiceGenerator() {
}

public static <S> S createService(Class<S> serviceClass, String baseUrl) {
    return createService(serviceClass, baseUrl, null, null);
}

public static <S> S createService(Class<S> serviceClass, String baseUrl, final String accesstoken, final String tokenType) {

    class MyErrorHandler implements ErrorHandler {
        @Override public Throwable handleError(RetrofitError cause) {
            return cause;
        }
    }

    Gson gson = new GsonBuilder()
            .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERscoreS)
            .registerTypeAdapter(Date.class, new DateTypeAdapter())
            .disableHtmlEscaping()
            .create();

    RestAdapter.Builder builder = new RestAdapter.Builder()
            .setEndpoint(baseUrl)
            .setClient(new OkClient(new OkHttpClient()))
            .setErrorHandler(new MyErrorHandler())
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setConverter(new GsonConverter(gson));

    if (accesstoken != null) {
        builder.setRequestInterceptor(new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                request.addHeader("Accept", "application/json;versions=1");
                request.addHeader("Authorization", tokenType +
                        " " + accesstoken);
            }
        });
    }

    RestAdapter adapter = builder.build();

    return adapter.create(serviceClass);
}

并使用:

    UploadService uploadService = ServiceGenerator.createService(UploadService.class,UploadService.BASE_URL);
uploadService.uploadFile(remUrl,typedFile,new CallbackInstance());

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐