ios - 使用 xcode 从 s3 访问数据

标签 ios objective-c database xcode amazon-s3

我不太清楚如何使用 XCode 从 S3 下载数据。任何有关如何执行此操作的帮助将不胜感激。我尝试使用以下代码从 S3 访问图像,

AmazonCredentials *cred = [[Amazon alloc] initWithAccessKey:accessKey withSecretKey:secretAccessKey];
AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithCredentials:cred];
S3GetObjectRequest *s3Request = [[S3GetObjectRequest alloc] initWithKey:urlPath      withBucket:bucket];
s3Request.delegate = self;
S3GetObjectResponse *s3Response = [s3 getObject:s3Request];

NSData*data = s3Response.body;
image = [UIImage imageWithData:data];

当我运行程序时,出现异常“EXC_BAD_ACCESS(代码 = 2,地址 = 0x0)”。我也不确定存储桶名称中到底包含什么内容。存储桶字符串应该只是“nameOfBucket”吗?或者类似“topLevelFolder/nameOfBucket”之类的东西?另外,“urlPath”中具体应该包含什么?我认为我的异常可能与不正确的存储桶和 urlPath 名称有关。

编辑:我发现我们没有从 S3 获取任何数据,解决方案是删除“s3Request.delegate = self;”行。

最佳答案

这里有两个用于获取图像和检查图像是否存在的辅助方法

#import <AWSiOSSDK/S3/AmazonS3Client.h>

+(NSData *)getImage:(NSString *)imageID inFolder:(NSString *)folderName
{
// Initial the S3 Client.
//folderName = bucket name
AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
s3.timeout = 1000;

@try {


    NSString *pictName = [NSString stringWithFormat:@"%@%@", imageID, @".jpg"];

    S3GetObjectRequest *porr = [[S3GetObjectRequest alloc] initWithKey:pictName withBucket:folderName];
    // Get the image data from the specified s3 bucket and object.
    S3GetObjectResponse *response = [s3 getObject:porr]; 

    NSData *imageData = response.body;

    return imageData;

}
@catch (AmazonClientException *exception) {
    return nil;
}
}



+(BOOL)isImageExists:(NSString *)imageID inFolder:(NSString *)folderName
{
@try {

    AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
    NSString *pictName = [NSString stringWithFormat:@"%@%@", imageID, @".jpg"];
    NSLog(@"Start checking a full size image %@", imageID);

    S3GetObjectMetadataRequest *porr = [[S3GetObjectMetadataRequest alloc] initWithKey:pictName withBucket:folderName];

    S3GetObjectResponse *response = [s3 getObjectMetadata:porr];

    if(response)
    {
        return YES;
    }
}
@catch (AmazonServiceException *ex) {
    NSLog(@"AmazonServiceException in isImageExists %@", ex.description);
    return NO;
}
@catch (NSException *exception) {
    NSLog(@"NSException in isImageExists %@", exception.description);
    return NO;
}

return NO;
}

关于ios - 使用 xcode 从 s3 访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16805225/

相关文章:

ios - 如果我重新创建推送通知证书,是否需要向 iOS 应用程序发送更新?

database - 我们可以在两个操作系统之间复制 postgres 数据吗?

java - 在android中存储大量图像

database - 如何排除 hibernate 中返回的某些列

ios - 'UINavigationController ?' does not have a member named ' 导航栏'

android - 恰好在午夜更新应用程序

iphone - 音频文件不使用 iphone sdk 播放

iOS 运行时 : Use Case

ios - Swift 中的单例和 NSNumberFormatter

ios - ObjC-以编程方式将标签添加到UIScrollView