Uploading files (video files) to S3 server from an

2019-05-07 02:11发布

I am trying to upload video files to a Bucket in S3 server from android app using a signed URLs which is generated from server side (coded in python) application. We are making a PUT request to the signed URL but we are getting

connection reset by peer exception.

But when I try the same URL on the POSTMAN REST CLIENT get a success message. Any help will be appreciated.

2条回答
Juvenile、少年°
2楼-- · 2019-05-07 03:00

Use dynamic URL instead of providing the base URL, use @Url instead of @Path and pass a complete URI, encode= false is by default

Eg:

@Multipart @PUT @Headers("x-amz-acl:public-read") Call<Void> uploadFile(@Url String url, @Header("Content-Type") String contentType, @Part MultipartBody.Part part);

查看更多
地球回转人心会变
3楼-- · 2019-05-07 03:02

Done this using Retrofit HTTP client library,it successfully uploaded file to Amazon s3 server.

code:

 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);
}

service class

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);
}

and use:

    UploadService uploadService = ServiceGenerator.createService(UploadService.class,UploadService.BASE_URL);
uploadService.uploadFile(remUrl,typedFile,new CallbackInstance());
查看更多
登录 后发表回答