0
我目前正試圖通過使用Alamofire上傳圖像預簽名URL上傳對象,從亞馬遜的例子實施如下:http://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObject.htmlAlamofire和
.NET版本代碼:
private void UploadS3FileByPresignedUrl(string url, string localFile, string contentType)
{
using (var client = new HttpClient())
{
using (var stream = File.OpenRead(localFile))
{
//// Need to add contentType otherwise the StreamContent is "binary/octet-stream"
var fileContent = new StreamContent(stream);
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
var response = client.PutAsync(url, fileContent).Result;
response.EnsureSuccessStatusCode();
}
}
}
基本上我必須上傳2條件的圖像:
- 只支持jpg圖像文件。
- 客戶端後,即可獲取上傳的URL,需要使用與內容類型「圖像/ JPEG」的HTTP PUT上傳個人資料圖片下面是.NET客戶端示例代碼
我想通過使用Alamofire這樣做,但我一直不成功...
我使用SWIFT 3 Alamofire 4,目前我有這樣的代碼:
let image = UIImage(named: "Sky-Sunset")
Alamofire.upload(multipartFormData:{ multipartFormData in
multipartFormData.append(UIImageJPEGRepresentation(image!, 0.5)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")},
usingThreshold:UInt64.init(),
to:".../my/profile/image/uploadurl",
method:.put,
headers:["Content-Type": "image/jpeg"],
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})