0
は現在、私はアマゾンからの実装例は以下の通りです、Alamofireを用いて画像をアップロードしようとしています:http://docs.aws.amazon.com/AmazonS3/latest/dev/PresignedUrlUploadObject.htmlAlamofireと事前署名したURLのアップロードオブジェクト
.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クライアント
のコード例でありますiOS用AWS SDKを使用して、私はお勧め
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)
}
})