如何使用 java 将文件上传到 AWS 中的预签名 URL?

URL url = new URL("https://prod-us-west-2-uploads.s3-us-west-2.amazonaws.com/arn%3Aaws%3Adevicefarm%3Aus-west-2%3A225178842088%3Aproject%3A1e6bbc52-5070-4505-b4aa-592d5e807b15/uploads/arn%3Aaws%3Adevicefarm%3Aus-west-2%3A225178842088%3Aupload%3A1e6bbc52-5070-4505-b4aa-592d5e807b15/501fdfee-877b-42b7-b180-de584309a082/Hamza-test-app.apk?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20181011T092801Z&X-Amz-SignedHeaders=host&X-Amz-Expires=86400&X-Amz-Credential=AKIAJSORV74ENYFBITRQ%2F20181011%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Signature=f041f2bf43eca1ba993fbf7185ad8bcb8eccec8429f2877bc32ab22a761fa2a");

        File file = new File("C:\\Users\\Hamza\\Desktop\\Hamza-test-app.apk");

        //Create Connection

        HttpURLConnection connection =  (HttpURLConnection) url.openConnection();

        connection.setDoOutput(true);

        connection.setRequestMethod("PUT");

        BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream());

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

        int i;

        // read byte by byte until end of stream

        while ((i = bis.read()) > 0) {

    bos.write(i);

        }

        bos.flush();

        bis.close();

        bos.close();



        System.out.println("HTTP response code: " + connection.getResponseCode());

    }catch(Exception ex){

        System.out.println("Failed to Upload File");

    }

我想将文件上传到 Java 中的 aws 农场设备,但文件未上传到 aws 项目上传列表。


繁花不似锦
浏览 297回答 3
3回答

慕勒3428872

只是为了详细说明我后面的评论,这里有两个示例如何上传到 Device Farm 的 SDK 在 java 中返回的预签名 URL。下面是一个将文件上传到 Device Farm s3 预签名 URL 的示例:package com.jmp.stackoveflow;import java.io.File;import java.io.IOException;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import com.amazonaws.ClientConfiguration;import com.amazonaws.auth.AWSSessionCredentials;import com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider;import com.amazonaws.services.devicefarm.*;import com.amazonaws.services.devicefarm.model.CreateUploadRequest;import com.amazonaws.services.devicefarm.model.Upload;import org.apache.commons.lang3.RandomStringUtils;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpPut;import org.apache.http.entity.FileEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;public class App {    public static void main(String[] args) {        String PROJECT_ARN = "arn:aws:devicefarm:us-west-2:111122223333:project:ffb3d9f2-3dd6-4ab8-93fd-bbb6be67b29b";        String ROLE_ARN = "arn:aws:iam::111122223333:role/DeviceFarm_FULL_ACCESS";        System.out.println("Creating credentials object");        // gettting credentials        STSAssumeRoleSessionCredentialsProvider sts = new STSAssumeRoleSessionCredentialsProvider.Builder(ROLE_ARN,                RandomStringUtils.randomAlphanumeric(8)).build();        AWSSessionCredentials creds = sts.getCredentials();        ClientConfiguration clientConfiguration = new ClientConfiguration()                .withUserAgent("AWS Device Farm - stackoverflow example");        AWSDeviceFarmClient api = new AWSDeviceFarmClient(creds, clientConfiguration);        api.setServiceNameIntern("devicefarm");        System.out.println("Creating upload object");        File app_debug_apk = new File(                "PATH_TO_YOUR_FILE_HERE");        FileEntity fileEntity = new FileEntity(app_debug_apk);        CreateUploadRequest appUploadRequest = new CreateUploadRequest().withName(app_debug_apk.getName())                .withProjectArn(PROJECT_ARN).withContentType("application/octet-stream").withType("ANDROID_APP");        Upload upload = api.createUpload(appUploadRequest).getUpload();        // Create the connection and use it to upload the new object using the        // pre-signed URL.        CloseableHttpClient httpClient = HttpClients.createSystem();        HttpPut httpPut = new HttpPut(upload.getUrl());        httpPut.setHeader("Content-Type", upload.getContentType());        httpPut.setEntity(fileEntity);        try {            HttpResponse response = httpClient.execute(httpPut);            System.out.println("Response: "+ response.getStatusLine().getStatusCode());        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}输出Creating credentials objectCreating upload objectResponse: 200

吃鸡游戏

您应该使用AWS SDK如图所示这里

缥缈止盈

这是一个有点老的问题。万一其他人发现了这个。这是我如何解决小于 5mb 的文件的问题。对于超过 5mb 的文件,建议使用分段上传。注意:使用 Java 的“尝试资源”很方便。Try Catch 使这成为一个笨拙的操作,但它确保在方法中以最少的代码量关闭资源。/**&nbsp;* Serial upload of an array of media files to S3 using a presignedUrl.&nbsp;*/public void serialPutMedia(ArrayList<String> signedUrls) {&nbsp; &nbsp; &nbsp; &nbsp; long getTime = System.currentTimeMillis();&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.debug("serialPutMedia called");&nbsp; &nbsp; &nbsp; &nbsp; String toDiskDir = DirectoryMgr.getMediaPath('M');&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpURLConnection connection;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < signedUrls.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; URL url = new URL(signedUrls.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection = (HttpURLConnection) url.openConnection();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection.setDoOutput(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection.setRequestMethod("PUT");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; localURL = toDiskDir + "/" + fileNames.get(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try (BufferedInputStream bin = new BufferedInputStream(new FileInputStream(new File(localURL)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(connection.getOutputStream())))&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.debug("S3put request built ... sending to s3...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] readBuffArr = new byte[4096];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int readBytes = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((readBytes = bin.read(readBuffArr)) >= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.write(readBuffArr, 0, readBytes);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connection.getResponseCode();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.debug("response code: {}", connection.getResponseCode());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.warn("\tFile Not Found exception");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.warn(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (MalformedURLException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.warn(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.warn(e.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; getTime = (System.currentTimeMillis() - getTime);&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Total get time in syncCloudMediaAction: {" + getTime + "} milliseconds, numElement: {" + signedUrls.size() + "}");&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java