S3图片上传、删除等函数的使用

1、S3上传
    AWSS3 *AWSS3Manager = [AWSS3 S3ForKey:CNNorth1S3];
    AWSS3PutObjectRequest *getLog = [[AWSS3PutObjectRequest alloc] init];
    getLog.bucket = [[UserInfo shareUserInfo].original objectForKey:@"bucket"];
    getLog.key = key;
    getLog.contentType = @"image/jpeg";
    long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.uploadfileArray[i] error:nil][NSFileSize] longLongValue];
    getLog.body = [NSURL fileURLWithPath:self.uploadfileArray[i]];
    getLog.contentLength = [NSNumber numberWithUnsignedLongLong:fileSize];
    NSData *data = [self.uploadfileArray[i] dataUsingEncoding:NSUTF8StringEncoding];
    NSString *fileNameBase64 = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];           // base64格式的字符串
   [[AWSS3Manager putObject:getLog] continueWithBlock:^id(AWSTask *task) {
  dispatch_async(dispatch_get_main_queue(), ^{
         if(task.error) {
                 DLOG(@"Error: %@",task.error);
           } else {
                  DLOG(@"Got here: %@", task.result);
           }
    });
        return nil;
    }];
}
2、删除函数
 AWSS3 *s3 = [AWSS3 S3ForKey:CNNorth1S3];
AWSS3DeleteObjectRequest *deleteRequest = [AWSS3DeleteObjectRequest new];
deleteRequest.bucket = bucket;
deleteRequest.key = key;
[[[s3 deleteObject:deleteRequest] continueWithBlock:^id(AWSTask *task) {
        awsdeleteCallBlock(task);
        return nil;
}] waitUntilFinished];

3、得到文件名称
AWSS3 *AWSS3Manager = [AWSS3 S3ForKey:CNNorth1S3];
NSString *key = [NSString stringWithFormat:@"%@/%@", [[UserInfo shareUserInfo].exif objectForKey:@"keybase"], contentID];
AWSS3GetObjectRequest *getLog = [[AWSS3GetObjectRequest alloc] init];
getLog.bucket = [[UserInfo shareUserInfo].exif objectForKey:@"bucket"];
getLog.key = key;
[[AWSS3Manager getObject:getLog] continueWithBlock:^id(AWSTask *task) {
     if(task.error) {
            DLOG(@"Error: %@",task.error); failBlock([task.error.userInfo objectForKey:@"Message"]);
        } else {
            DLOG(@"Got here: %@", task.result);
            AWSS3GetObjectOutput *outPut = task.result;
            NSData *jsonData = outPut.body;
            NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
            NSString *name = [responseJSON objectForKey:@"filename"];
            successed(name);
        }
        return nil;
    }];
4、获取图片信息
AWSS3 *AWSS3Manager = [AWSS3 S3ForKey:CNNorth1S3];
NSString *key = [NSString stringWithFormat:@"%@/%@", [[UserInfo shareUserInfo].exif objectForKey:@"keybase"], contentID];
AWSS3GetObjectRequest *getLog = [[AWSS3GetObjectRequest alloc] init];
getLog.bucket = [[UserInfo shareUserInfo].exif objectForKey:@"bucket"];
getLog.key = key;
 [[AWSS3Manager getObject:getLog] continueWithBlock:^id(AWSTask *task) {
        if(task.error) {
            DLOG(@"Error: %@",task.error);
            failBlock([task.error.userInfo objectForKey:@"Message"]);
        } else {
            DLOG(@"Got here: %@", task.result);
            AWSS3GetObjectOutput *outPut = task.result;
            successed(outPut);
        }
        return nil;
    }];
5、获取图片链接
AWSS3GetPreSignedURLRequest *getPreSignedURLRequest = [AWSS3GetPreSignedURLRequest new];
getPreSignedURLRequest.bucket = [[UserInfo shareUserInfo].middle objectForKey:@"bucket"];
getPreSignedURLRequest.key = [NSString stringWithFormat:@"%@/%@",  [[UserInfo shareUserInfo].middle objectForKey:@"keybase"], imageName];
getPreSignedURLRequest.HTTPMethod = AWSHTTPMethodGET;
 getPreSignedURLRequest.expires = [NSDate dateWithTimeIntervalSinceNow:3600];
//S3函数调用
 AWSS3PreSignedURLBuilder *S3PreSignedURLBuilder =
 [AWSS3PreSignedURLBuilder S3PreSignedURLBuilderForKey:CNNorth1S3PreSignedURLBuilder];
 [[S3PreSignedURLBuilder getPreSignedURL:getPreSignedURLRequest] continueWithBlock:^id(AWSTask *task) {
       if (task.error) {//get image URL false
            DLOG(@"Error: %@",task.error);
            NSData *data = task.error.userInfo[@"com.alamofire.serialization.response.error.data"];
            [self getErrorString:data withUrl:@"" withSatusCode:0];
        } else { //get image URL successed
            NSURL *presignedURL = task.result;
            DLOG(@"download presignedURL is: \n%@", presignedURL);
            callBack(presignedURL);
        }
        return nil;
    }];
6、STS
KWS(weakSelf)
AWSStaticCredentialsProvider *credentialsProvider = [[AWSStaticCredentialsProvider alloc] init];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionCNNorth1 credentialsProvider:credentialsProvider];
 [AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
NSDictionary *param = @{@"RoleArn" : @"arn:aws-cn:iam::329403576641:role/CanonIDasOpenID",
                                @"RoleSessionName" : @"CanonID",
                                @"WebIdentityToken" : [UserInfo shareUserInfo].idToken,
                                @"DurationSeconds" : [NSNumber numberWithInt:3600]
                                };
AWSSTSAssumeRoleWithWebIdentityRequest *request = [[AWSSTSAssumeRoleWithWebIdentityRequest alloc] initWithDictionary:param error:nil];
 [[AWSSTS defaultSTS] assumeRoleWithWebIdentity:request completionHandler:^(AWSSTSAssumeRoleWithWebIdentityResponse * _Nullable response, NSError * _Nullable error) {
            //将值存入
            [UserInfo shareUserInfo].accessKeyId = response.credentials.accessKeyId;
            [UserInfo shareUserInfo].secretAccessKey = response.credentials.secretAccessKey;
            [UserInfo shareUserInfo].sessionToken = response.credentials.sessionToken;
            [UserInfo shareUserInfo].expiration = response.credentials.expiration;
            AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionCNNorth1 credentialsProvider:credentialsProvider];
            [AWSS3PreSignedURLBuilder registerS3PreSignedURLBuilderWithConfiguration:configuration forKey:CNNorth1S3PreSignedURLBuilder];
            [AWSS3 registerS3WithConfiguration:configuration forKey:CNNorth1S3];
            [AWSS3TransferUtility registerS3TransferUtilityWithConfiguration:configuration forKey:CNNorth1DownloadS3];
            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isFIrst"] == YES) {
                if(awsSTSCallBack){
                    awsSTSCallBack(YES);
                }
            } else {
                //重新获取一次getBucketInfo
                [weakSelf createGetBucketInfo:^(BOOL isGetBucketInfo) {
                    if(awsSTSCallBack){
                        awsSTSCallBack(isGetBucketInfo);
                    }
                }];
            }
        }];

你可能感兴趣的:(S3图片上传、删除等函数的使用)