可能是它让,但可能会帮助一些之一。这里是上传图像女将-S3代码
constant.h
#import <Foundation/Foundation.h>
#define SECRET_KEY @"your secret kry"
#define ACCESS_KEY_ID @"your access id"
的#define PICTURE_BUCKET @ “斗名” 的#define PICTURE_NAME @ “任何词” 的#define CREDENTIALS_ERROR_TITLE @ “缺少凭据” 的#define CREDENTIALS_ERROR_MESSAGE @” AWS Credentials配置不正确,请查阅自述文件。“
@interface常数:NSObject的
+(的NSString *)pictureBucket;
@end
在constant.m
#进口“常数。H”
@implementation常数 +(的NSString *)pictureBucket { 返回[[NSString的stringWithFormat:@ “%@ - @%”,PICTURE_BUCKET,ACCESS_KEY_ID] lowercaseString]; }
@end
在UploadViewController.h
typedef enum {
GrandCentralDispatch,
Delegate,
BackgroundThread
} UploadType;
@interface UploadPhotoViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UINavigationControllerDelegate, AmazonServiceRequestDelegate>
{
UploadType _uploadType;
}
@property (strong, nonatomic) IBOutlet UIButton *buttonSelectPhoto;
@property (nonatomic, retain) AmazonS3Client *s3;
在UploadViewController.m
#import "UploadPhotoViewController.h"
#import <AWSRuntime/AWSRuntime.h>
#import "Constant.h"
@interface UploadPhotoViewController()
@end
@implementation UploadPhotoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
#ifdef DEBUG
[AmazonLogger verboseLogging];
#else
[AmazonLogger turnLoggingOff];
#endif
[AmazonErrorHandler shouldNotThrowExceptions];
if(![ACCESS_KEY_ID isEqualToString:@"CHANGE ME"]
&& self.s3 == nil)
{
// Initial the S3 Client.
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// This sample App is for demonstration purposes only.
// It is not secure to embed your credentials into source code.
// DO NOT EMBED YOUR CREDENTIALS IN PRODUCTION APPS.
// We offer two solutions for getting credentials to your mobile App.
// Please read the following article to learn about Token Vending Machine:
// * http://aws.amazon.com/articles/Mobile/4611615499399490
// Or consider using web identity federation:
// * http://aws.amazon.com/articles/Mobile/4617974389850313
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
self.s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY] ;
self.s3.endpoint = [AmazonEndpoints s3Endpoint:US_WEST_2];
// Create the picture bucket.
S3CreateBucketRequest *createBucketRequest = [[S3CreateBucketRequest alloc] initWithName:[Constant pictureBucket] andRegion:[S3Region USWest2]];
S3CreateBucketResponse *createBucketResponse = [self.s3 createBucket:createBucketRequest];
if(createBucketResponse.error != nil)
{
NSLog(@"Error: %@", createBucketResponse.error);
}
}
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([ACCESS_KEY_ID isEqualToString:@"CHANGE ME"])
{
[self showAlertMessage:CREDENTIALS_ERROR_MESSAGE withTitle:CREDENTIALS_ERROR_TITLE];
}
} - (无效)showAlertMessage:(的NSString *)消息withTitle:(的NSString *)标题 { UIAlertView中* alertView = [[UIAlertView中的alloc] initWithTitle:标题 消息:消息 代表:无 cancelButtonTitle: @“OK” otherButtonTitles:nil]; alertView.delegate = self; [alertView show]; } - (空)alertView:(UIAlertView中*)alertView clickedButtonAtIndex:(NSInteger的)buttonIndex {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
// Set the content type so that the browser will treat the URL as an image.
S3ResponseHeaderOverrides *override = [[S3ResponseHeaderOverrides alloc] init];
override.contentType = @"image/jpeg";
// Request a pre-signed URL to picture that has been uplaoded.
S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest alloc] init];
gpsur.key = PICTURE_NAME;
gpsur.bucket = [Constant pictureBucket];
gpsur.expires = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval) 3600]; // Added an hour's worth of seconds to the current time.
gpsur.responseHeaderOverrides = override;
// Get the URL
NSError *error = nil;
NSURL *url = [self.s3 getPreSignedURL:gpsur error:&error];
if(url == nil)
{
if(error != nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Error: %@", error);
[self showAlertMessage:[error.userInfo objectForKey:@"message"] withTitle:@"Browser Error"];
});
}
}
else
{
/*dispatch_async(dispatch_get_main_queue(), ^{
// Display the URL in Safari
[[UIApplication sharedApplication] openURL:url];
});*/
}NSLog(@"url is %@",url);
});
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonTakePhoto:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (IBAction)buttonSelectPhoto:(id)sender
{
[self showImagePicker:Delegate];
}
- (void)showImagePicker:(UploadType)uploadType
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init] ;
imagePicker.delegate = self;
_uploadType = uploadType;
[self presentViewController:imagePicker animated:YES completion:NULL];
}
- (void)processDelegateUpload:(NSData *)imageData
{
// Upload image data. Remember to set the content type.
S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:PICTURE_NAME
inBucket:[Constant pictureBucket]];
por.contentType = @"image/jpeg";
por.data = imageData;
por.delegate = self;
// Put the image data into the specified s3 bucket and object.
[self.s3 putObject:por];
}
-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
{
[self showAlertMessage:@"The image was successfully uploaded." withTitle:@"Upload Completed"];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
-(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", error);
[self showAlertMessage:error.description withTitle:@"Upload Error"];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
#pragma mark - UIImagePickerControllerDelegate methods
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the selected image.
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Convert the image to JPEG data.
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
if(_uploadType == Delegate)
{
[self processDelegateUpload:imageData];
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (IBAction)buttonBack:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
@end
你可能要重新考虑这样做,而是在iOS应用和S3之间添加一个Web服务层。否则,您将需要将您的AWS凭证放入应用程序本身。因此,如果有人侵入您的应用,他们可以完全访问您的S3存储(以及该密钥可用的任何其他Amazon服务)。 – 2012-08-02 21:21:06
我已经想过了,已经设置了TVM Web服务层......但是我无法通过开始测试TVM步骤的第一步......我想如果我能够得到上面的代码工作,我可以向前推进(并提出更多问题!)以获得TVM工作 - 思考?除非你有整个事情的建议! – 2012-08-02 21:30:46
嘿,你解决了这个问题吗?我在这里面临同样的问题。 – codingrhythm 2013-12-16 00:59:03