【问题标题】:aws lambda file upload through api gateway in java通过java中的api网关上传aws lambda文件
【发布时间】:2018-02-07 14:20:16
【问题描述】:

您能帮我通过 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;
}

【问题讨论】:

  • 为什么不上传到 S3?

标签: java lambda


【解决方案1】:

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

this

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

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

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

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

【讨论】:

    猜你喜欢
    • 2021-03-16
    • 1970-01-01
    • 2019-05-27
    • 2016-09-02
    • 2021-10-23
    • 2018-07-24
    • 2021-12-20
    • 2023-02-22
    • 2023-02-22
    相关资源
    最近更新 更多