前后端分离,如何使用Ucloud当作静态资源服务器

以下以上传图片到ucloud

题记:当app或者前端在执行上传图片到ucloud服务器的时候,需要先来java服务器端接口做一次鉴权,获得许可,然后就可以上传图片到uclould了.

1.首先要在pom文件中增加ucloud依赖配置

        
        <dependency>
            <groupId>com.ucloudgroupId>
            <artifactId>ufile_sdkartifactId>
            <version>1.0.0version>
        dependency>
        <dependency>
            <groupId>org.apache.httpgroupId>
            <artifactId>httpcoreartifactId>
            <version>4.1.4version>
        dependency>
        <dependency>
            <groupId>org.apachegroupId>
            <artifactId>httpclientartifactId>
            <version>4.1.3version>
        dependency>
        <dependency>
            <groupId>commons-httpclientgroupId>
            <artifactId>commons-httpclientartifactId>
            <version>3.1version>
        dependency>

2.建一个controller

@RestController
@RequestMapping("/app/common")
public class CommonAppController extends BaseAppController {
    @Autowired
    private UCloudService uCloudService;

@PostMapping("/signUploadContent")
    public JsonResult signUploadContent(String contentMD5, String contentType, String fileName) {
        //获取头像上传的ucloud的数据签名
        UploadSignature uploadSignature = uCloudService.sign(contentMD5, contentType, fileName, FileTypeEnum.AVATAR);

        return success(uploadSignature);
    }
    }

3.ucloud 的服务接口

public interface UCloudService {

    UploadSignature sign(String contentMD5, String contentType, String fileName, FileTypeEnum fileType);
}

4.ucloud的实现类

@Service
@Configuration
public class UCloudServiceImpl implements UCloudService{


    @Value("${ucloud.publicKey}")
    private String publicKey;

    @Value("${ucloud.privateKey}")
    private String privateKey;

    @Value("${ucloud.bucket}")
    private String bucket;

    @Value("${ucloud.proxySuffix}")
    private String proxySuffix;

    @Value("${ucloud.cdn.https.host}")
    private String cdnHttpsHost;

    private static HmacSHA1 hmacSHA1 = new HmacSHA1();

    /**
     *
     * stringToSign =  httpMethod + "\n" + contentMD5 + "\n" + contentType + "\n" + date + "\n" +
     * 如 stringToSign = "PUT\n1f0d1bbab0442b1c893d4a0476396321\napplication/jpeg\n\n/gamecat-pkg/abs.jpg"
     * canonicalUCloudHeaders + canonicalResource;
     * signature = HmacSha1.sign(privateKey + stringToSign)
     * authorization = “UCloud ${publicKey}:${signature}
     *
     * @param contentMD5
     * @param contentType
     * @return
     */
    @Override
    public UploadSignature sign(String contentMD5, String contentType, String fileName, FileTypeEnum fileType) {

        StringBuilder stringToSign = new StringBuilder("PUT");
        stringToSign.append("\n");
        stringToSign.append(contentMD5);
        stringToSign.append("\n");
        stringToSign.append(contentType);
        stringToSign.append("\n\n");
        stringToSign.append("/");
        stringToSign.append(bucket);
        stringToSign.append("/");

        /**key值**/
        StringBuilder key = new StringBuilder(fileType.getPrefix());
        key.append("/").append(UUID.randomUUID().toString());

        if (-1 != fileName.lastIndexOf(".")) {
            key.append(".").append(fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()));
        }
        stringToSign.append(key.toString());

        /**authoriztion值**/
        String signature = hmacSHA1.sign(privateKey, stringToSign.toString());
        StringBuilder authorization = new StringBuilder("UCloud");
        authorization.append(" ");
        authorization.append(publicKey).append(":").append(signature);

        /**下载链接**/
        StringBuilder downloadPath = new StringBuilder();
        downloadPath. append(cdnHttpsHost).
                append("/").
                append(key.toString());


        UploadSignature uploadSignature = new UploadSignature();
        uploadSignature.setKey(key.toString());
        uploadSignature.setAuthorization(authorization.toString());
        uploadSignature.setDownloadPath(downloadPath.toString());
        uploadSignature.setBucket(bucket);
        uploadSignature.setProxySuffix(proxySuffix);

        return uploadSignature;
    }
}

5.实现类加载的配置文件(请根据个人需要填写)

#ucloud
ucloud.publicKey=公钥
ucloud.privateKey=私
ucloud.proxySuffix=
ucloud.bucket=
ucloud.cdn.https.host=

你可能感兴趣的:(云存储)