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