ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

随笔:接入腾讯云图片存储实战

2021-03-20 09:31:28  阅读:233  来源: 互联网

标签:credential property 接入 self NSString 腾讯 nonatomic 随笔 void


一、需求

客户端为了合规访问的图片必须经过加密才能访问,项目组决定对接腾讯云cos服务。

腾讯云COS服务是将图片保存到腾讯云服务器中,客户端将图片上传上去,返回一个客户端自定义的图片地址,客户端将图片传给cos服务器,cos服务器将临时访问的图片链接给到客户端,客户端展示给用户看。如果中间人hook到自定义图片地址是无法得到该图片的,这样就可以确保项目服务器个人信息安全。

二、接入实操

001 图片都是存入专有的bucket中,可以理解为一个专有子目录。

002 注册COS服务 这里注册一次就行 注册参数例如 appID regionName 设置请求代理等

003 通过服务器接口的到访问专有bucket的一些参数例如secretID secretKey token experationDate等 将这些参数请求腾讯云授权服务

004 得到访问授权后 将我们的图片传入服务器 这里需要配置一些参数:桶的名称 ,自定义图片key ,上传图片路径等

这里我建立一个工具类文件便于使用

//
//  QCloudTools.h
//  
//
//  Created by maochengfang on 2021/3/16.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface QCloudTools : NSObject

SingletonH();

@property (nonatomic, assign) long long expiredTime;
@property (nonatomic, copy) NSString* filePath;
@property (nonatomic, copy) NSString* region;
@property (nonatomic, copy) NSString* sessionToken;
@property (nonatomic, copy) NSString* tmpSecretId;
@property (nonatomic, copy) NSString* tmpSecretKey;
@property (nonatomic, copy) void(^uploadImageCloudPath)(NSString* path);

@property (nonatomic, strong) void (^presinedImageCloudPath)(NSDictionary *imageDict);


- (void)registerCOSXMLService;
- (void)setUp;
/**
 * 高级接口上传对象
 */
- (void)transferUploadFile:(NSString *)filePath;

/*
 获取下载预览图片
 */
- (void)PutObjectWithPresignedURL:(NSString *)fileURL;
@end

NS_ASSUME_NONNULL_END
//
//  QCloudTools.m
//  
//
//  Created by maochengfang on 2021/3/16.
//

#import "QCloudTools.h"

@interface QCloudTools ()<QCloudSignatureProvider, QCloudCredentailFenceQueueDelegate>
@property (nonatomic) QCloudCredentailFenceQueue* credentialFenceQueue;
@end

@implementation QCloudTools

SingletonM();

- (void)setUp {
    // 注册默认的 COS 服务
    QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];
    //关闭读取系统剪贴板的功能
    configuration.appID = @"";
    configuration.signatureProvider = self;
    QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];
    endpoint.regionName = kRegion;
    endpoint.useHTTPS = YES;
    configuration.endpoint = endpoint;
   
    [QCloudCOSXMLService registerDefaultCOSXMLWithConfiguration:configuration];
    [QCloudCOSTransferMangerService registerDefaultCOSTransferMangerWithConfiguration:configuration];
    
    self.credentialFenceQueue = [QCloudCredentailFenceQueue new];
    self.credentialFenceQueue.delegate = self;
    [QCloudCOSXMLConfiguration sharedInstance].currentRegion = kRegion;
    QCloudServiceConfiguration* configuration1 = [[QCloudCOSXMLService defaultCOSXML].configuration copy];
    configuration1.endpoint.regionName = kRegion;
    [QCloudCOSTransferMangerService registerCOSTransferMangerWithConfiguration:configuration1 withKey:kRegion];
    
    [QCloudCOSXMLService registerCOSXMLWithConfiguration:configuration1 withKey:kRegion];
  
    self.isRegister = YES;
   
}

- (void)registerCOSXMLService{
  
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self fetchData];
    });
}

- (void) fenceQueue:(QCloudCredentailFenceQueue * )queue requestCreatorWithContinue:(QCloudCredentailFenceQueueContinue)continueBlock
{
    QCloudCredential* credential = [QCloudCredential new];
    //在这里可以同步过程从服务器获取临时签名需要的 secretID,secretKey,expiretionDate 和 token 参数
    credential.secretID = self.tmpSecretId;
    credential.secretKey = self.tmpSecretKey;
    credential.token = self.sessionToken;
    
    /*强烈建议返回服务器时间作为签名的开始时间,用来避免由于用户手机本地时间偏差过大导致的签名不正确 */
//    credential.startDate = [NSDate dateWithTimeIntervalSince1970:[SADateFormatTool getCurrentTimestamp]]; // 单位是秒
    credential.experationDate = [NSDate dateWithTimeIntervalSince1970:self.expiredTime];;
    QCloudAuthentationV5Creator* creator = [[QCloudAuthentationV5Creator alloc]
        initWithCredential:credential];
    continueBlock(creator, nil);
}

- (void) signatureWithFields:(QCloudSignatureFields*)fileds
                     request:(QCloudBizHTTPRequest*)request
                  urlRequest:(NSMutableURLRequest*)urlRequst
                   compelete:(QCloudHTTPAuthentationContinueBlock)continueBlock
{
    //在这里可以同步过程从服务器获取临时签名需要的 secretID,secretKey,expiretionDate 和 token 参数
    
    QCloudCredential* credential = [QCloudCredential new];
    credential.secretID = self.tmpSecretId;
    credential.secretKey = self.tmpSecretKey;
    credential.token = self.sessionToken;
    credential.experationDate = [NSDate dateWithTimeIntervalSince1970:self.expiredTime];;
    QCloudAuthentationV5Creator* creator = [[QCloudAuthentationV5Creator alloc] initWithCredential:credential];
    QCloudSignature* signature =  [creator signatureForData:urlRequst];
    NSLog(@"_signature===%@&x-cos-security-token=%@",signature.signature,self.sessionToken);
    continueBlock(signature, nil);
}


- (void)fetchData {
    
//  根据用户信息: 获取当前用户所有地区下的存储桶列表,
//    实例化 QCloudGetServiceRequest
//    调用 QCloudCOSXMLService 实例的 GetService 方法 发起请求
//    在FinishBlock获取结果
    QCloudGetServiceRequest* request = [QCloudGetServiceRequest new];
    DEF_WeakSelf(self)
    [request setFinishBlock:^(QCloudListAllMyBucketsResult * _Nullable result, NSError * _Nullable error) {
        
        NSLog(@"result=====%@",result);
        
    }];
    
    [[QCloudCOSXMLConfiguration sharedInstance].currentService GetService:request];
}


/**
 * 高级接口上传对象
 */
- (void)transferUploadFile:(NSString *)filePath{
    
    //.cssg-snippet-body-start:[objc-transfer-upload-file]
    QCloudCOSXMLUploadObjectRequest* put = [QCloudCOSXMLUploadObjectRequest new];
    // 本地文件路径
    NSURL* url = [NSURL fileURLWithPath:filePath];
    put.accessControlList = @"default";
    // 存储桶名称,格式为 BucketName-APPID
    put.bucket = @"";
    
    // 对象键,是对象在 COS 上的完整路径,如果带目录的话,格式为 "dir1/object1"
    put.object = [NSString stringWithFormat:@"/%@ios-%ld.png",self.filePath,[SADateFormatTool getCurrentTimestamp]];
    
    // 需要上传的对象内容。可以传入NSData*或者NSURL*类型的变量
    put.body =  url;
    WS(weakSelf);
    
    // 监听上传结果
    [put setFinishBlock:^(QCloudUploadObjectResult* outputObject, NSError *error) {
        // 可以从 outputObject 中获取 response 中 etag 或者自定义头部等信息
        NSDictionary * result = (NSDictionary *)outputObject;
        [SVProgressHUD dismissWithDelay:1];
        if (!error) {
            
            NSLog(@"下载链接:%@\n\n",outputObject.location);

        }
    }];
    
    [put setSendProcessBlock:^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
        dispatch_async(dispatch_get_main_queue(), ^{
//            bytesSent  本次发送的字节数
//            totalBytesSent  总共发送的字节数
//            totalBytesExpectedToSend  总共需要发送的字节数(即整个文件的大小)
            
//            [weakself.progressView setProgress:(1.0f*totalBytesSent)/totalBytesExpectedToSend animated:YES];
            NSString* str = [NSString stringWithFormat:@"上传中(%.0f%%)",1.0f*totalBytesSent/totalBytesExpectedToSend * 100];
            
            [SVProgressHUD showWithStatus:str];
            NSLog(@"progressView ====%@",str);
        });
    }];
    
    [[QCloudCOSTransferMangerService defaultCOSTransferManager] UploadObject:put];
    
    //.cssg-snippet-body-end
}

- (void)PutObjectWithPresignedURL:(NSString *)fileURL{
    
    NSString* objct =  [fileURL stringByReplacingOccurrencesOfString:@"https://qiangdan-1255789551.cos.ap-shanghai.myqcloud.com" withString:@""];
    NSLog(@"currentThread---%@",[NSThread currentThread]);
    
    QCloudGetPresignedURLRequest* getPresignedURLRequest = [[QCloudGetPresignedURLRequest alloc] init];
    getPresignedURLRequest.bucket = [NSString stringWithFormat:@"%@-1255789551",@"qiangdan"];;
    getPresignedURLRequest.HTTPMethod = @"GET";
    getPresignedURLRequest.object = objct;
    WS(weakSelf);
   
    [getPresignedURLRequest setFinishBlock:^(QCloudGetPresignedURLResult * _Nullable result, NSError * _Nullable error) {
            
       
        // 预签名 URL
            NSString* presignedURL = result.presienedURL;

    }];
    
    [[QCloudCOSXMLService defaultCOSXML] getPresignedURL:getPresignedURLRequest];
}


@end

三、总结

仔细文档多和同事一起讨论

官方文档

官方文档1

标签:credential,property,接入,self,NSString,腾讯,nonatomic,随笔,void
来源: https://blog.csdn.net/Coding_Physical/article/details/115021714

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有