简体   繁体   English

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

[英]Upload Database file from ios to AWS s3 issue

Hi i try to upload a file to my bucket at AWS s3 without successes. 嗨,我尝试将文件上传到AWS s3上的存储桶但未成功。

I just need to upload a file from some application from some phones. 我只需要从某些手机的某些应用程序上传文件。

i use AWS SDK version2 on ios 8. 我在ios 8上使用AWS开发工具包版本2。

this is my function for Upload a DB: 这是我上传数据库的功能:

UPLOAD 上传

 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 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;
}

** **

Thanks 谢谢

Try changing your bucket policy to this (replace bucketName): 尝试将您的存储桶策略更改为此(替换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/*"
    }
]
}

This allows you to Add/Read objects from your bucket, you should read AWS S3 Documentation in order to know what exactly you want your policy to be, but for now, this should do the trick. 这使您可以从存储桶中添加/读取对象,您应该阅读AWS S3文档 ,以便确切了解您想要的策略是什么,但是现在,这应该可以解决问题。

I give up on using AWS SDK . 我放弃使用AWS SDK I have tried with AFAmazonS3Manager and It works well. 我已经尝试过使用AFAmazonS3Manager ,并且效果很好。

AFAmazonS3Manager : AFNetworking Client for the Amazon S3 API. AFAmazonS3Manager :Amazon S3 API的AFNetworking客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM