2
當我試圖通過命令行aws s3 cp /Users/shravan40/Downloads/scan1.jpg s3://s3_bucket_name/access_key_id
成功上傳圖像上傳文件到我的s3存儲桶時。但是,當我嘗試通過Scala API上傳圖像時,它顯示網關超時。我已經把時間設定爲2分鐘。S3桶連接失敗(HTTP/1.1 504 GATEWAY_TIMEOUT)
public void connect(){
conn = new AmazonS3Client(credentials);
}
public AmazonS3Client(AWSCredentials awsCredentials) {
this(awsCredentials, new ClientConfiguration());
}
public void setBucket(String bucketName){
this.bucketName = bucketName;
this.baseUrl = "https://s3.us-west-2.amazonaws.com/"+bucketName;
}
這裏是API代碼,
post("/api/:version/user/:id/image/upload") {
CGMetrics.apiProfiler.profile("api.uploadImage") {
val memberId: Long = params("id").toLong
if (!AccountService.validateMemberId(memberId))
APIResponseError(APIResponseCode.NOT_FOUND, "Invalid Member Id")
else {
val filePath: String = getUploadedFilePath("file_path", "file")
val fileName: String = getParam("filename").get.toString
if (filePath != null) {
val (uploadImageUrl, resizedImageHeight, resizedImageWidth) = MediaService.uploadImage(memberId, 0, fileName, filePath)
APIResponseOK(Map("imgUrl" -> uploadImageUrl, "imgHeight" -> resizedImageHeight, "imgWidth" -> resizedImageWidth))
} else {
APIResponseError(APIResponseCode.UNPROCESSABLE_ENTITY, "No File Found")
}
}
}
}
而且getUploadFilePath方法,
private def getUploadedFilePath(filePathKey: String = "file_path", fieldName: String = "file")(implicit request: HttpServletRequest): String = {
val filePath: String = getParam(filePathKey) match {
case Some(x) => x
case None => fileParams.get(fieldName) match {
case Some(file) =>
val fileName = file.getName
val ext = Helper.getFileExtension("/tmp/" + fileName)
Log.logger(Log.FILE.DEFAULT).debug("file name = " + fileName)
Log.logger(Log.FILE.DEFAULT).debug("file ext = " + ext)
val tempFile = File.createTempFile(fileName, "." + ext)
Helper.writeToFile(tempFile, file.get())
Log.logger(Log.FILE.DEFAULT).info("Created Temporary Asset : " + fileName + " at " + tempFile.getAbsolutePath)
tempFile.getAbsolutePath
case None =>
null
}
}
filePath
}
更新:現在文件被上傳到S3桶,但沒有確認爲同。
有什麼建議嗎?
訪問桶不與HTTP協議完成的默認端口號,但與S3協議,例如。您的存儲區網址應類似於s3:// s3.us-west-2.amazonaws.com/bucketName'。如果你想用http訪問它,你可以在你的存儲桶上啓用靜態虛擬主機。 –
@MartinHansen:我嘗試了s3://s3.us-west-2.amazonaws.com/bucketName,但回覆仍然保持不變。 – Shravan40
只是使用's3:// bucketName'(就是我所做的) - 我只是複製粘貼你的解決方案,並以s3://協議交換http://爲例。我不知道你的鬥完整的uri :) –