You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AliyunCloudStorageService.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Copyright (c) 2018 人人开源 All rights reserved.
  3. *
  4. * https://www.renren.io
  5. *
  6. * 版权所有,侵权必究!
  7. */
  8. package io.renren.cloud;
  9. import java.io.ByteArrayInputStream;
  10. import java.io.InputStream;
  11. import com.aliyun.oss.OSSClient;
  12. import io.renren.common.exception.ErrorCode;
  13. import io.renren.common.exception.RenException;
  14. /**
  15. * 阿里云存储
  16. *
  17. * @author Mark sunlightcs@gmail.com
  18. */
  19. public class AliyunCloudStorageService extends AbstractCloudStorageService {
  20. public AliyunCloudStorageService(CloudStorageConfig config){
  21. this.config = config;
  22. }
  23. @Override
  24. public String upload(byte[] data, String path) {
  25. return upload(new ByteArrayInputStream(data), path);
  26. }
  27. @Override
  28. public String upload(InputStream inputStream, String path) {
  29. OSSClient client = new OSSClient(config.getAliyunEndPoint(), config.getAliyunAccessKeyId(),
  30. config.getAliyunAccessKeySecret());
  31. try {
  32. client.putObject(config.getAliyunBucketName(), path, inputStream);
  33. client.shutdown();
  34. } catch (Exception e){
  35. throw new RenException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e, "");
  36. }
  37. return config.getAliyunDomain() + "/" + path;
  38. }
  39. @Override
  40. public String uploadSuffix(byte[] data, String suffix) {
  41. return upload(data, getPath(config.getAliyunPrefix(), suffix));
  42. }
  43. @Override
  44. public String uploadSuffix(InputStream inputStream, String suffix) {
  45. return upload(inputStream, getPath(config.getAliyunPrefix(), suffix));
  46. }
  47. }