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

Upload Database file from ios to AWS s3 issue

您好,我尝试将文件上传到我在 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;
}

**

谢谢

尝试将您的存储桶策略更改为此(替换 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/*"
    }
]
}

这允许您从存储桶中 Add/Read 对象,您应该阅读 AWS S3 Documentation 以了解您想要的政策究竟是什么,但就目前而言,这应该可以解决问题。

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

AFAmazonS3Manager:Amazon S3 的 AFNetworking 客户端 API。