将数据库文件从ios上传到AWS s3问题

时间:2015-02-23 10:16:28

标签: ios objective-c amazon-web-services amazon-s3 ios8.1

您好我尝试在AWS s3上将文件上传到我的存储桶而没有成功。

我只需要从某些手机的某个应用程序上传文件。

我在ios 8上使用AWS SDK version2。

这是我上传数据库的功能:

上传

 NSString *fileName = DB_File;
        NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *filePath = [directoryPath stringByAppendingPathComponent:fileName];
    NSData *fileData = [NSData dataWithContentsOfFile:filePath];





    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
    AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
    uploadRequest.body = [NSURL fileURLWithPath:filePath];
    uploadRequest.key = fileName;
    uploadRequest.bucket = @"xxxx";

    [[transferManager upload:uploadRequest] continueWithBlock:^id(BFTask *task) {
        if (task.error) {
            if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
                switch (task.error.code) {
                    case AWSS3TransferManagerErrorCancelled:
                    case AWSS3TransferManagerErrorPaused:
                    {
                     NSLog(@"Upload failed: [%@]", task.error);
                    }
                        break;

                    default:
                        NSLog(@"Upload failed: [%@]", task.error);
                        break;
                }
            } else {
                NSLog(@"Upload failed: [%@]", task.error);
            }
        }

        if (task.result) {
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"Upload ok: [%@]", task.error);
            });
        }
        return nil;
    }];

AppDelege.m

**

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
    AWSCognitoCredentialsProvider *credentialsProvider = [AWSCognitoCredentialsProvider
                                                          credentialsWithRegionType:AWSRegionxxxx
                                                          identityPoolId:@"us-xxxx-1:xxxxxx-xxxx-xxxx-xxxxx-xxxxxxx"];

    AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSEast1
                                                                          credentialsProvider:credentialsProvider];

    [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;


    return YES;
}

**

由于

2 个答案:

答案 0 :(得分:0)

尝试将您的存储分区策略更改为此(替换bucketName):

{
"Version": "2008-10-17",
"Id": "policy",
"Statement": [
    {
        "Sid": "allow-public-read",
        "Effect": "Allow",
        "Principal": {
            "AWS": "*"
        },
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::bucketName/*"
    },
    {
        "Sid": "allow-public-put",
        "Effect": "Allow",
        "Principal": {
            "AWS": "*"
        },
        "Action": "s3:PutObject",
        "Resource": "arn:aws:s3:::bucketName/*"
    }
]
}

这允许您从存储桶中添加/读取对象,您应该阅读AWS S3 Documentation以了解您希望策略的确切内容,但是现在,这应该可以解决问题。

答案 1 :(得分:0)

我放弃使用AWS SDK。我试过AFAmazonS3Manager并且效果很好。

AFAmazonS3Manager:适用于Amazon S3 API的AFNetworking客户端。