2023年6月25日发(作者:)
第四章:minio的presignedURLs上传⽂件章节快捷访问:第⼀章:minio介绍与安装第⼆章:minio单机版,使⽤客户端备份⽂件第三章:minio的javaAPI第四章:minio的presigned URLs上传⽂件--------------------------------------------------
当我们通过java的API上传⽂件的时候就会发现,我们把java的API封装了⼀下,提供了⼀个接⼝给其他应⽤调⽤,那么整个的上传流程就变成了“应⽤客户端”-->“JavaAPI端”-->“minio服务端”。中间通过JavaAPI转了⼀次。⽐如我们的“应⽤客户端”是web浏览器的时候,能不能直接从浏览器上传到“minio服务端”呢?答案是可以的,minio有提供JSSDK,我们可以通过JSAPI直接上传到“minio服务端”。JSSDK参考官⽹的⽂档:var Minio = require('minio')// Instantiate the minio client with the endpoint// and access keys as shown minioClient = new ({ endPoint: '', port: 9000, useSSL: true, accessKey: 'Q3AM3UQ867SPQQA43P2F', secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'});// File that needs to be file = '/tmp/'// Make a bucket called cket('europetrip', 'us-east-1', function(err) { if (err) return (err) ('Bucket created successfully in "us-east-1".') var metaData = { 'Content-Type': 'application/octet-stream', 'X-Amz-Meta-Testing': 1234, 'example': 5678 } // Using fPutObject API upload your file to the bucket europetrip. ject('europetrip', '', file, metaData, function(err, etag) { if (err) return (err) ('File uploaded successfully.') });});根据官⽹的⽂档,我们可以看到,从js上传⽂件需要将我们的accessKey跟secretKey暴露出去。这样的话,别⼈拿到我们的key岂不是可以为所欲为了!这样是很危险的,所以我们不能这样做。除⾮你是⽤这段脚本来写服务,把他当作服务端。但是这样的话,岂不是⼜是通过“应⽤客户端”-->“api端”-->“minio服务端”这样的路径来进⾏上传了。presigned URLsminio并没有像商⽤的七⽜云存储那样,可以通过临时的token来上传⽂件。但是,这个但是很重要啊,他提供了使⽤pre-signed URLs通过浏览器上传⽂件。那么这个pre-signed URLs是啥⼦?pre-signed URLs简单来说就是预处理签名的⼀个URL。就是url中包含了签名信息,但是是经过加密的。⽽且还有时效性的。这样⼀来就跟七⽜云的临时token有异曲同⼯之处了。既保证了⽤最短的途径能上传⽂件,⼜保证了我们的上传签名信息不会被恶意利⽤。官⽹的⽂档:这⾥官⽹给出的例⼦是的,我们使⽤Java该怎么⽤呢!⾸先在我们之前的MinioTemplate类中,加上⽀持⽅法:/** * 获得上传的URL * @param bucketName * @param objectName * @param expires * @return * @throws IOException * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws InsufficientDataException * @throws InvalidExpiresRangeException * @throws InternalException * @throws NoResponseException * @throws InvalidBucketNameException * @throws XmlPullParserException * @throws ErrorResponseException */ if(expires == null){ // 7天 return nedPutObject(bucketName,objectName); } return nedPutObject(bucketName,objectName,expires);}public String presignedPutObject(String bucketName, String objectName, Integer expires) throws IOException, InvalidKeyException, NoSuchAlgorithmException, I然后在我们之前的控制器中加上⽅法:/** * 获得上传的URL * @param bucketName ⽂件桶名称 * @param objectName ⽂件对象名称:(pdf/2019/0512/) */@ApiOperation(value = "获得上传的URL", response = )@GetMapping("/uploadUrl")public ActionResult
发布者:admin,转转请注明出处:http://www.yc00.com/news/1687678064a30807.html
评论列表(0条)