我是亚马逊s3的新手,我正在尝试使用预先签名的url上传图像到我的s3存储桶。它显示我访问被拒绝。我的网址格式是
https://bucketname.s3.amazonaws.com/imagename.jpeg&AWSAccessKeyId=XXXX&Expires=XXXX&Signature=XXXX
我在互联网上搜索并发现签名网址不需要上传存储桶策略,因此我没有创建任何存储桶策略。
我在python中编写了一段代码:
class PreSignImage(Resource):
results = fields.DictField(attribute='aws_string')
class Meta:
object_class=FeedObject
allowed_methods = ['get']
always_return_data = True
authentication = Authentication()
serializer = Serializer(formats=['json'])
authorization = Authorization()
include_resource_uri=False
resource_name = 'upload_image'
always_return_data = True
def get_object_list(self, bundle):
results = list()
file_name=bundle.request.GET['file_name']
file_type=bundle.request.GET['file_type']
s3=boto3.client('s3',aws_access_key_id=settings.AWS_ACCESS_KEY,aws_secret_access_key=settings.AWS_SECRET_KEY)
presigned_post = s3.generate_presigned_post(Bucket = settings.S3_BUCKET,
Key = file_name,
Fields = {"acl": "public-read", "Content-Type": file_type},
Conditions = [
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn = 36000
)
screenobj = FeedObject()
screenobj.aws_string=presigned_post
results.append(screenobj)
#data=json.dumps({'data': presigned_post,'url': 'https://%s.s3.amazonaws.com/%s' % (settings.S3_BUCKET, file_name)})
#response=HttpResponse(content=data,status=200,content_type="application/json;")
#response['Content-Length']=len(data)
return results
尝试从ios应用上传图片:
-(void) uploadImage:(UIImage *)image atUrl:(NSString *)url withMimeType:(NSString *)mimeType {
NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
NSURL *requestURL = [NSURL URLWithString:url];
AFHTTPSessionManager *client = [[AFHTTPSessionManager alloc] initWithBaseURL:requestURL];
client.requestSerializer=[AFJSONResponseSerializer serializer];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"PUT"];
[request setValue:mimeType forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:imageData];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[imageData length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"public-read" forHTTPHeaderField:@"x-amz-acl"];
[request setURL:requestURL];
NSURLSessionDataTask *task = [client dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[task resume];
}
过去2天仍然无法将图像上传到我的存储桶中。请帮忙