我尝试使用Swift将文件上传到S3,但我无法建立连接。如何从S3查看错误详细信息以找出我的上传尝试无效的原因。
传递给S3的参数:
["X-Amz-Date": 20160916T010311Z,
"X-Amz-Signature":qr647bMEA9M8LeBVqkJLcvEIO4g=,
"trackURL": https://bucketname.s3.amazonaws.com/fileID,
"key": Track50, "acl": public-read,
"policy": wtLepKMeghI3rt1UeikLoQcNTYtMDktMTBUMTc6NDE6MTJaIiwgImNvbmRpdGlvbnMiOiBbeyJhY2wiOiAicHVibGljLXJlYWQifSwgeyJDb250ZW50LVR5cGUiOiAibTRhIn0sIHsiYnVja2V0IjogImZ1eZEaJKYzeDgZwz3lOPtys5B0MigIwImv2OpQm3ylP==,
"Content-Type": m4a,
"AWSAccessKeyId": XXXXXXXXXXXX,
"X-Amz-Credential": AWSAccessKeyId/2016915/us-west-1/s3/aws4_request,
"X-Amz-Algorithm": AWS4-HMAC-SHA256]
错误我回来了:
response:<NSHTTPURLResponse: 0x7f8713ed7090> { URL: https://bucketname.s3.amazonaws.com/ } { status code: 400, headers {
Connection = close;
"Content-Type" = "application/xml";
Date = "Fri, 16 Sep 2016 03:36:42 GMT";
Server = AmazonS3;
"Transfer-Encoding" = Identity;
"x-amz-id-2" = "rzvVE96GNdVadls1wGigWYVsyoowITSI9sqWuEQ2IYIYsPdlV8HDlKQa0APQjwJ3Yxikp9L1ozU=";
"x-amz-request-id" = 196F566C41386F62;
} }
ErrorReason(code: -2, message: "http error code 400")
设置请求的代码
protocol FileUpload: Requestable {
var fileURL: NSURL { get }
var mimeType: String { get }
var request: NSURLRequest { get }
}
extension FileUpload {
var request: NSURLRequest {
let URLString = "https://bucketname.s3.amazonaws.com"
let URL = NSURL(string:URLString)
let r = NSMutableURLRequest(URL:URL!)
r.HTTPMethod = method.rawValue
let boundary = generateBoundaryString()
r.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
r.HTTPBody = createBodyWithParameters(params, boundary: boundary)
return r
}
func createBodyWithParameters(parameters: [String: AnyObject]?, boundary: String) -> NSData {
let body = NSMutableData()
if parameters != nil {
for (key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
let data = NSData(contentsOfURL: fileURL)
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"file\"; filename=\"\(fileURL.lastPathComponent!)\"\r\n")
body.appendString("Content-Type: \(mimeType)\r\n\r\n")
body.appendData(data!)
body.appendString("\r\n")
body.appendString("--\(boundary)--\r\n")
return body
}
func handleResponse(data: NSData?, response: NSURLResponse?, error: NSError?) -> Result<Serialize> {
if let httpResponse = response as? NSHTTPURLResponse {
print("response:\(httpResponse)")
if httpResponse.statusCode == 200 {
do {
let json: JSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
return Serialize.instantiate(json)
}
catch {
print("Error thrown. Could not parse JSON: '\(error)'")
return Result(error:ErrorReason(code: -1, message: "invalid json stream \(error)"))
}
}
print("error under handle response")
return Result(error:ErrorReason(code: -2, message: "http error code \(httpResponse.statusCode)"))
}
return Result(error: ErrorReason(code:-2, message: "Unable to reach server"))
}
答案 0 :(得分:0)
你应该像下面那样设置存储桶策略和cors配置;
单击“编辑存储桶策略”按钮并编写此
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AddPerm",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::YOURBUCKET/*"
}
]
}
点击编辑cors配置按钮并写下此行
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>