记录uniapp使用华为云obs上传图片视频

1:去华为云官网下载  OBS BrowserJS SDK  华为云


 2:项目中引入sdk文件

import ObsClient from '../../utils/esdk-obs-browserjs-without-polyfill.3.22.3.min.js';

3:配置使用

videoUpload() {
			const that = this;
			uni.chooseVideo({
				sourceType: ['camera'],
				success: function(res) {
					// 年与日
					const now = new Date();
					const year = now.getFullYear();
					let month = now.getMonth() + 1;
					let day = now.getDate();
                    // 如果日期小于10,前面补0
					if (month < 10) {
						month = '0' + month;
					}

					// 如果日期小于10,前面补0
					if (day < 10) {
						day = '0' + day;
					}
					const my_data = `${year}${month}${day}`;
					const newName = new Date().valueOf(); //当前时间戳
					const str10 = that.s10(); //随机10字符
					var arr = res.tempFile.name.split('.');
					const str = arr[0];
					const encodedUrl = encodeURIComponent(str);
					uni.showLoading({
						title: '上传中'
					});
                    var obsClient = new ObsClient({
	                    access_key_id: '你的ak', 
	                    secret_access_key: '你的sk',
	                    server: 'https://obs.cn-north-4.myhuaweicloud.com' // 你的endPoint
                    });
					obsClient.putObject(
						{
							Bucket: '你的桶名',
							Key: `video/${my_data}/${newName}_${str20}.${arr[1]}`, // 路径 + 文件名
							SourceFile: res.tempFile
						},
						function(err, result) {
							if (!err) {
                               console.log(`https://${这里是Bucket}.obs.cn-north-4.myhuaweicloud.com/video/${my_data}/${newName}_${str20}.${arr[1]}`);
								that.$nextTick(() => {
									uni.hideLoading();
								});
							}
						}
					);
				}
			});
		},

4:这里是上面我用到的随机10字符串

// 随机10字符
		s10() {
			var data = [
				'0',
				'1',
				'2',
				'3',
				'4',
				'5',
				'6',
				'7',
				'8',
				'9',
				'A',
				'B',
				'C',
				'D',
				'E',
				'F',
				'G',
				'H',
				'I',
				'J',
				'K',
				'L',
				'M',
				'N',
				'O',
				'P',
				'Q',
				'R',
				'S',
				'T',
				'U',
				'V',
				'W',
				'X',
				'Y',
				'Z',
				'a',
				'b',
				'c',
				'd',
				'e',
				'f',
				'g',
				'h',
				'i',
				'j',
				'k',
				'l',
				'm',
				'n',
				'o',
				'p',
				'q',
				'r',
				's',
				't',
				'u',
				'v',
				'w',
				'x',
				'y',
				'z'
			];

			var result = '';
			for (var i = 0; i < 10; i++) {
				var r = Math.floor(Math.random() * 62); 
				result += data[r]; 
			}
			return result;
		}

注意:

  1. 多看官方文档 !!!
  2. 我是上传到 /video/当前年月日(如:20230425)/当前时间戳_随机10字符.文件后缀,并且修改了文件名字,这样确保文件唯一性,
  3. 取值的时候就是
  4. 'https://' + bucket + '.obs.cn-north-4.myhuaweicloud.com/' + key

  5. 这里的key就是咱们存的时候的路径加文件名和文件后缀,取出来之后就是线上预览链接
  6. 还是多多看文档!!!!!!!!!!!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你可以使用 Spring Boot 和华为云 OBS SDK 来上 MultipartFile 对象到华为云 OBS。下面是一个示例代码: 1. 首先,你需要在 pom.xml 文件中添加华为云 OBS 的依赖: ```xml <dependency> <groupId>com.obs</groupId> <artifactId>obs-sdk-java</artifactId> <version>3.20.0</version> </dependency> ``` 2. 然后,你需要配置华为云 OBS 的相关信息,如 accessKey、secretKey、endpoint 等。你可以将这些信息放在 application.properties 或 application.yml 文件中,如: ```yaml huawei: obs: accessKey: yourAccessKey secretKey: yourSecretKey endpoint: yourEndpoint bucketName: yourBucketName ``` 3. 接下来,创建一个 Service 类来处理文件上的逻辑: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import com.obs.services.ObsClient; import com.obs.services.model.PutObjectRequest; import java.io.IOException; @Service public class ObsService { @Value("${huawei.obs.accessKey}") private String accessKey; @Value("${huawei.obs.secretKey}") private String secretKey; @Value("${huawei.obs.endpoint}") private String endpoint; @Value("${huawei.obs.bucketName}") private String bucketName; public void uploadFile(MultipartFile file) throws IOException { ObsClient obsClient = new ObsClient(accessKey, secretKey, endpoint); PutObjectRequest request = new PutObjectRequest(bucketName, file.getOriginalFilename(), file.getInputStream()); obsClient.putObject(request); obsClient.close(); } } ``` 4. 最后,在你的 Controller 中使用 ObsService 类来处理文件上请求: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @RestController public class FileUploadController { @Autowired private ObsService obsService; @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { try { obsService.uploadFile(file); return "File uploaded successfully."; } catch (IOException e) { return "Failed to upload file."; } } } ``` 以上就是使用 Spring Boot 和华为云 OBS SDK 上 MultipartFile 的示例代码。确保你已经正确配置了华为云 OBS 的相关信息,并根据你的实际情况进行修改。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值