从oss桶下的目录获取图片url信息,并导入数据库

 @Override
    public TableDataInfo<SpeciesDictionaryVo> queryPageList(SpeciesDictionaryBo bo, PageQuery pageQuery) {
        List<SpeciesDictionaryVo> speciesDictionaryVos = baseMapper.selectVoList(null);

        // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        // 填写Bucket名称,例如examplebucket。
        String bucketName = "kyoeis-yunpintai";
        // 指定前缀,例如exampledir/object。
        String keyPrefix = "speciesDictionary/";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, "LTAI5t9zoxyVhzQwBSY1EHQC","jzY2VBnXsaP1w3XuMqVTbmhwl6h0tD");

        // 存储所有图片URL
        List<String> imageUrls = new ArrayList<>();
        try {
            // 列举文件。如果不设置keyPrefix,则列举存储空间下的所有文件。如果设置keyPrefix,则列举包含指定前缀的文件。
            ObjectListing objectListing = ossClient.listObjects(bucketName, keyPrefix);
            objectListing.setMaxKeys(500);
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                String objectKey = s.getKey();
                System.out.println(s.getLastModified()+""+s.getBucketName());
                // 假设此处使用简单的方式判断是否为图片,实际应用中可能需要更复杂的逻辑(如检查MIME类型)
                if (objectKey.endsWith(".jpg") || objectKey.endsWith(".png")) {
                    String imageUrl = "https://" + bucketName + "." + "oss-cn-hangzhou.aliyuncs.com" + "/" + objectKey;
                    System.out.println(objectKey);

                    String[] split = objectKey.split("/");
                    String[] split1 = split[1].split("\\.");
                    System.out.println(split1[0]);

                    for (SpeciesDictionaryVo speciesDictionaryVo : speciesDictionaryVos) {
                        if(speciesDictionaryVo.getSpeciesChineseName().equals(split1[0])){
                            speciesDictionaryVo.setSpeciesImageUrl(imageUrl);
                            System.out.println(speciesDictionaryVo.getSpeciesImageUrl());
                            SpeciesDictionary bean = BeanUtil.toBean(speciesDictionaryVo, SpeciesDictionary.class);
                            LambdaQueryWrapper<SpeciesDictionary> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                            lambdaQueryWrapper.eq(SpeciesDictionary::getId,bean.getId());
                            baseMapper.update(bean,lambdaQueryWrapper);

                        }
                    }
                }
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with OSS, "
                + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return TableDataInfo.build(speciesDictionaryVos);
    }

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Spring Boot中,你可以使用Amazon S3 SDK来将OSS获取URL存入数据库。 首先,你需要在你的pom.xml文件中添加Amazon S3 SDK依赖: ``` <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> <version>1.11.1003</version> </dependency> ``` 然后,在你的代码中使用以下步骤将OSS获取URL存入数据库: 1. 创建Amazon S3客户端: ``` AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey))) .withRegion(region) .build(); ``` 其中,accessKey和secretKey是你的OSS访问密钥,region是OSS所在的地域信息。 2. 获取文件URL: ``` String url = s3Client.getUrl(bucketName, objectKey).toString(); ``` 其中,bucketName是你的OSS存储名称,objectKey是存储对象的唯一标识符。 3. 将URL存入数据库: 你可以使用Spring Data JPA或者MyBatis等ORM框架将URL存入数据库。例如,使用Spring Data JPA: ``` @Entity @Table(name = "file") public class FileEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String url; // getters and setters } ``` ``` @Repository public interface FileRepository extends JpaRepository<FileEntity, Long> { } ``` ``` @Service public class FileService { @Autowired private FileRepository fileRepository; public void saveFile(String name, String url) { FileEntity file = new FileEntity(); file.setName(name); file.setUrl(url); fileRepository.save(file); } } ``` 在保存文件时,调用saveFile方法将文件名和URL存入数据库即可。 注意,以上代码仅供参考,具体实现可能需要根据你的项目需求进行调整。 ### 回答2: 在Spring Boot中将OSS获取URL存入数据库,可以通过以下步骤实现: 1. 配置阿里云OSS SDK依赖:在项目的pom.xml文件中添加阿里云OSS SDK的依赖,确保能够使用OSS相关的API。 2. 创建数据库表:根据需求,在数据库中创建一个表,用于存储OSS获取URL。表中可以包含字段如id、url等。 3. 创建实体类:在Java代码中创建与数据库表对应的实体类,例如创建一个OssUrl实体类,包含与表字段对应的属性。 4. 编写处理逻辑:在需要使用OSS获取URL并存入数据库的地方,编写相应的处理逻辑。可以使用OSS SDK提供的API上传文件到OSS,并获取该文件的URL。 5. 将URL存入数据库获取URL后,可以使用Spring Data JPA等持久化框架的方法,将URL存入数据库中。 具体实现代码示例如下: ```java // OssUrl 实体类 @Entity public class OssUrl { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String url; // 省略getter和setter方法 } // 服务类 @Service public class OssUrlService { @Autowired private OssUrlRepository ossUrlRepository; @Autowired private OSS ossClient; // 阿里云OSS客户端 public void saveOssUrl(String objectName) { // 上传文件到OSS // 获取文件URL String url = ossClient.generatePresignedUrl(bucketName, objectName, expiration).toString(); // 将URL存入数据库 OssUrl ossUrl = new OssUrl(); ossUrl.setUrl(url); ossUrlRepository.save(ossUrl); } } ``` 需要注意的是,上述示例中的代码仅是一个基本的参考,具体实现还需要根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值