aws lambda文件通过java中的api网关上传

时间:2018-02-07 06:05:52

标签: java lambda

请帮我通过AWS lambda中的API网关上传文件

请您分享示例代码以便在Lambda中上传文件。

我面临解决lambda函数

中文件内容的问题

以下是我面临的问题

public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {

    try {

    /*  int c;
        StringBuffer a = new StringBuffer();
        while ((c = inputStream.read()) != -1) {
            a.append(c);
        }



        HttpClient httpClient = HttpClientBuilder.create().build();
        JSONObject accessTokenJson = getAccessToken(httpClient);
        JSONObject attachmentResponse = sendAttachmentToCc(inputStream, httpClient, accessTokenJson);
        sendResponse(attachmentResponse, outputStream);

    } catch (Exception e) {
        LOGGER.info("error in handle request" + e);
    }
}

private JSONObject sendAttachmentToCc(InputStream inputStream, HttpClient httpClient, JSONObject accessTokenJson)
        throws IOException {
    JSONObject responsejson = null;
    String status = null;
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
//  byte[] bytes = read((ByteArrayInputStream) inputStream);
    byte[] fileContent = IOUtils.toByteArray(inputStream);
    LOGGER.info("bytesread" + fileContent.length);
    ByteArrayBody bs = new ByteArrayBody(fileContent, "Jellyfish.jpg");
    builder.addPart("upload", bs);
    HttpEntity entity = builder.build();
    HttpPost request = new HttpPost(System.getenv("attachment_save_url"));
    request.setEntity(entity);
    request.addHeader("Authorization", "Bearer" + " " + accessTokenJson.getAsString("access_token"));
    try {
        HttpResponse httpResponce = httpClient.execute(request);
        String responseString = new BasicResponseHandler().handleResponse(httpResponce);
        LOGGER.info("attachment save response::::" + responseString);
        JsonObject convertedObject = (JsonObject) gson.fromJson(responseString, JsonObject.class);
        responsejson = popualteResponse(status, responseString, convertedObject);
    } catch (IOException e) {
        LOGGER.info("error in save attachment ::::" + e.getMessage());
    }
    return responsejson;
}

1 个答案:

答案 0 :(得分:0)

您的代码不符合Lambda处理程序。

请参阅this

您需要将此作为处理程序。

  

处理程序的一般语法如下:

     

outputType handler-name(inputType input,Context context){...}

要使API GW处理二进制数据,您需要使用此guide