### Configure Huawei OBS and Aliyun OSS Storage Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration examples for Huawei OBS and Aliyun OSS. Both require access keys, secret keys, endpoints, bucket names, and domain configurations for accessing stored files. ```yaml dromara: x-file-storage: #文件存储配置 default-platform: huawei-obs-1 #默认使用的存储平台 thumbnail-suffix: ".min.jpg" #缩略图后缀,例如【.min.jpg】【.png】 huawei-obs: - platform: huawei-obs-1 # 存储平台标识 enable-storage: true # 启用存储 access-key: ?? secret-key: ?? end-point: ?? bucket-name: ?? domain: ?? # 访问域名,注意“/”结尾,例如:http://abc.obs.com/ base-path: test/ # 基础路径 - platform: huawei-obs-2 # 存储平台标识,这与这里不能重复 enable-storage: true # 启用存储 access-key: ?? secret-key: ?? end-point: ?? bucket-name: ?? domain: ?? # 访问域名,注意“/”结尾,例如:http://abc.obs.com/ base-path: test2/ # 基础路径 aliyun-oss: - platform: aliyun-oss-1 # 存储平台标识 enable-storage: true # 启用存储 access-key: ?? secret-key: ?? end-point: ?? bucket-name: ?? domain: ?? # 访问域名,注意“/”结尾,例如:https://abc.oss-cn-shanghai.aliyuncs.com/ base-path: test/ # 基础路径 ``` -------------------------------- ### FastDFS Configuration (YAML) Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for FastDFS. Uses `run-mod` for path/filename handling. Requires tracker server addresses. `base-path` is recommended to be empty. ```yaml fastdfs: - platform: fastdfs-1 enable-storage: true run-mod: COVER tracker-server: server-addr: ?? domain: ?? base-path: "" ``` -------------------------------- ### Configure go-fastdfs Storage Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for integrating with go-fastdfs. Requires server address, group name, and scene. Timeout settings and domain for access are also configurable. ```yaml go-fastdfs: - platform: go-fastdfs-1 # 存储平台标识 enable-storage: true # 启用存储 server: # 服务端地址 例如http://172.26.11.44:10081 group: 'group1' # 服务端组名 scene: 'scene' #上传场景 time-out: 30000 #访问服务端超时时间 domain: '' # 访问域名,注意“/”结尾,例如:https://file.abc.com/ ramdom: true base-path: '' # 基础路径 ``` -------------------------------- ### Add UPYUN USS Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet includes the Maven dependency for integrating with UPYUN USS. Ensure the version is compatible with your project. ```xml com.upyun java-sdk 4.2.3 ``` -------------------------------- ### FTP Configuration (YAML) Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for FTP server integration. Requires host, port, user, and password. `domain` is for access, and `storage-path` specifies the directory on the FTP server. ```yaml ftp: - platform: ftp-1 enable-storage: true host: ?? port: 21 user: anonymous password: "" domain: ?? base-path: config/ storage-path: /www/wwwroot/file.abc.com/ ``` -------------------------------- ### Add Qiniu Cloud Kodo Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet includes the Maven dependency for integrating with Qiniu Cloud Kodo storage. Check for version compatibility. ```xml com.qiniu qiniu-java-sdk 7.12.1 ``` -------------------------------- ### Add SFTP Dependencies Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet provides the Maven dependencies required for SFTP integration, including jsch, hutool-extra, and commons-pool2. Ensure all versions are compatible. ```xml com.jcraft jsch 0.1.55 cn.hutool hutool-extra 5.8.28 org.apache.commons commons-pool2 2.11.1 ``` -------------------------------- ### Add WebDAV Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet shows the Maven dependency for integrating with WebDAV. Ensure the version is compatible with your project. ```xml com.github.lookfirst sardine 5.10 ``` -------------------------------- ### WebDAV Configuration (YAML) Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for WebDAV server integration. Requires server URL, user, and password. `domain` is for access, and `storage-path` specifies the directory on the WebDAV server. ```yaml webdav: - platform: webdav-1 enable-storage: true server: ?? user: ?? password: ?? domain: ?? base-path: webdav/ storage-path: / ``` -------------------------------- ### Configure Local Storage Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for basic local file storage. It specifies the base path for storage and an optional domain and path pattern for access. Not recommended for production use. ```yaml local: - platform: local-1 # 存储平台标识 enable-storage: true #启用存储 enable-access: true #启用访问(线上请使用 Nginx 配置,效率更高) domain: "" # 访问域名,例如:“http://127.0.0.1:8030/test/file/”,注意后面要和 path-patterns 保持一致,“/”结尾,本地存储建议使用相对路径,方便后期更换域名 base-path: D:/Temp/test/ # 存储地址 path-patterns: /test/file/** # 访问路径,开启 enable-access 后,通过此路径可以访问到上传的文件 ``` -------------------------------- ### SFTP Configuration (YAML) Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for SFTP server integration. Requires host, port, user, and password or private key path. `domain` is for access, and `storage-path` indicates the server directory. ```yaml sftp: - platform: sftp-1 enable-storage: true host: ?? port: 22 user: root password: ?? private-key-path: ?? domain: ?? base-path: sftpConfig/ storage-path: /www/wwwroot/file.abc.com/ ``` -------------------------------- ### Configure Local Plus Storage Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for an upgraded local file storage solution. Includes storage path, access domain, base path, and path patterns for accessing files. Recommended over basic local storage. ```yaml local-plus: - platform: local-plus-1 # 存储平台标识 enable-storage: true #启用存储 enable-access: true #启用访问(线上请使用 Nginx 配置,效率更高) domain: http://127.0.0.1:8080/file/ # 访问域名,例如:“http://127.0.0.1:8030/file/”,注意后面要和 path-patterns 保持一致,“/”结尾,本地存储建议使用相对路径,方便后期更换域名 base-path: local-plus/ # 基础路径 path-patterns: /file/** # 访问路径 storage-path: D:/Temp/ # 存储路径 ``` -------------------------------- ### Add MinIO Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet provides the Maven dependency for integrating with MinIO object storage. Check for version compatibility. ```xml io.minio minio 8.5.2 ``` -------------------------------- ### Add FTP Dependencies Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet includes the necessary Maven dependencies for integrating with FTP storage, including commons-net, hutool-extra, and commons-pool2. Verify version compatibility. ```xml commons-net commons-net 3.9.0 cn.hutool hutool-extra 5.8.28 org.apache.commons commons-pool2 2.11.1 ``` -------------------------------- ### Add Baidu Cloud BOS Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet shows the Maven dependency for integrating with Baidu Cloud BOS. Verify the version compatibility. ```xml com.baidubce bce-java-sdk 0.10.251 ``` -------------------------------- ### Mongo GridFS Maven Dependencies Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This section provides Maven dependencies for integrating with Mongo GridFS. It includes the standard MongoDB driver and an alternative for Spring Boot projects. Choose the appropriate dependency based on your project setup. ```xml org.mongodb mongodb-driver-sync 4.6.1 org.springframework.boot spring-boot-starter-data-mongodb ``` -------------------------------- ### Google Cloud Storage Configuration (YAML) Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for Google Cloud Storage. Requires project ID, bucket name, and credentials path. `domain` is for access, and `base-path` defines a prefix for stored files. ```yaml google-cloud-storage: - platform: google-cloud-storage-1 enable-storage: true project-id: ?? bucket-name: ?? credentials-path: file:/deploy/example-key.json domain: ?? base-path: test/ ``` -------------------------------- ### Add FastDFS Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet provides the Maven dependency for integrating with FastDFS. Refer to the compatibility notes for FastDFS before use. ```xml io.github.rui8832 fastdfs-client-java 1.30-20230328 ``` -------------------------------- ### Add Alibaba Cloud OSS Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet shows the Maven dependency required to integrate with Alibaba Cloud Object Storage Service (OSS). Verify the version compatibility. ```xml com.aliyun.oss aliyun-sdk-oss 3.16.1 ``` -------------------------------- ### go-fastdfs Maven Dependencies Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet shows the Maven dependencies required for go-fastdfs integration. It specifically lists Hutool dependencies for HTTP and JSON utilities, which are necessary for its operation. ```xml cn.hutool hutool-http 5.8.28 cn.hutool hutool-json 5.8.28 ``` -------------------------------- ### Youpaiyun USS Configuration Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This YAML configuration is for Youpaiyun USS (Unified Storage Service). It includes username, password, bucket name, domain, and base path. Ensure a unique `platform` identifier is used for this configuration. ```yaml upyun-uss: - platform: upyun-uss-1 # 存储平台标识 enable-storage: true # 启用存储 username: ?? password: ?? bucket-name: ?? domain: ?? # 访问域名,注意“/”结尾,例如:http://abc.test.upcdn.net/ base-path: test/ # 基础路径 ``` -------------------------------- ### Add Google Cloud Storage Dependencies Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet includes Maven dependencies for Google Cloud Storage integration. It also includes a specific version of Guava to resolve potential conflicts. ```xml com.google.cloud google-cloud-storage 2.20.1 com.google.guava guava 31.1-jre ``` -------------------------------- ### Qiniu Kodo Configuration Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This YAML snippet details the configuration for Qiniu Kodo storage. It includes access key, secret key, bucket name, domain, and base path. Ensure you use a unique platform identifier and refer to the Java configuration class for more options. ```yaml qiniu-kodo: - platform: qiniu-kodo-1 # 存储平台标识 enable-storage: true # 启用存储 access-key: ?? secret-key: ?? bucket-name: ?? domain: ?? # 访问域名,注意“/”结尾,例如:http://abc.hn-bkt.clouddn.com/ base-path: base/ # 基础路径 ``` -------------------------------- ### Configure Volcengine TOS Storage Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for Volcengine TOS (Object Storage Service). Requires access key, secret key, region, endpoint, and bucket name. A domain for accessing files and a base path are also specified. ```yaml volcengine-tos: # 火山引擎 TOS 存储 - platform: volcengine-tos-1 # 存储平台标识 enable-storage: true # 启用存储 access-key: ?? secret-key: ?? region: ?? # 地区 end-point: ?? # 终端节点 bucket-name: ?? # 桶名称 domain: ?? # 访问域名,注意"/"结尾,例如:https://your-bucket.tos-cn-beijing.volces.com/ base-path: tos/ # 基础路径 ``` -------------------------------- ### MinIO Configuration Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This YAML snippet provides the configuration for MinIO object storage. It requires access key, secret key, endpoint, bucket name, domain, and base path. Use a unique `platform` identifier for each MinIO instance. ```yaml minio: - platform: minio-1 # 存储平台标识 enable-storage: true # 启用存储 access-key: ?? secret-key: ?? end-point: ?? bucket-name: ?? domain: ?? # 访问域名,注意“/”结尾,例如:http://minio.abc.com/abc/ base-path: test/ # 基础路径 ``` -------------------------------- ### Alibaba Cloud OSS Configuration Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This YAML snippet configures the Alibaba Cloud OSS platform. It requires access key, secret key, endpoint, bucket name, domain, and base path. The `platform` identifier should be unique. Consult the provided Java class for additional parameters. ```yaml aliyun-oss: - platform: aliyun-oss-1 # 存储平台标识 enable-storage: true # 启用存储 access-key: ?? secret-key: ?? end-point: ?? bucket-name: ?? domain: ?? # 访问域名,注意“/”结尾,例如:https://abc.oss-cn-shanghai.aliyuncs.com/ base-path: test/ # 基础路径 ``` -------------------------------- ### POST /multipart/upload-large-file Source: https://context7.com/dromara/x-file-storage/llms.txt A comprehensive example demonstrating the entire process of uploading a large file using multipart upload, including initiation, chunking, part uploading, and completion. ```APIDOC ## POST /multipart/upload-large-file ### Description Handles the complete process of uploading a large file using multipart upload. It first initiates the upload, then uploads the file in chunks (e.g., 5MB parts), and finally completes the upload. ### Method POST ### Endpoint /multipart/upload-large-file ### Parameters #### Query Parameters None #### Request Body - **file** (multipart/form-data) - Required - The large file to be uploaded. ### Response #### Success Response (200) - **FileInfo** (object) - Information about the successfully uploaded large file. - **uploadId** (string) - The upload ID associated with the multipart upload. - **filename** (string) - The final filename of the uploaded file. - **size** (long) - The total size of the uploaded file. - **platform** (string) - The storage platform used. - **path** (string) - The storage path. - **contentType** (string) - The MIME type of the file. - **objectId** (string) - The associated object ID. - **objectType** (string) - The type of the associated object. - **url** (string) - The URL to access the uploaded file. #### Response Example ```json { "uploadId": "another-upload-id", "filename": "my_big_archive.zip", "size": 524288000, "platform": "aliyun-oss-1", "path": "large-files/", "contentType": "application/zip", "objectId": "archive-005", "objectType": "archive", "url": "http://your-storage-url/large-files/my_big_archive.zip" } ``` ``` -------------------------------- ### x-file-storage Application Configuration Base Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This YAML snippet shows the base configuration for the x-file-storage framework in your `application.yml` file. It sets the default storage platform and thumbnail suffix. Ensure proper indentation. ```yaml dromara: x-file-storage: #文件存储配置 default-platform: huawei-obs-1 #默认使用的存储平台 thumbnail-suffix: ".min.jpg" #缩略图后缀,例如【.min.jpg】【.png】 #对应平台的配置写在这里,注意缩进要对齐 ``` -------------------------------- ### Add x-file-storage Spring Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet shows how to add the core x-file-storage dependency for Spring Boot environments using Maven. Ensure you are using a compatible version with your project. ```xml org.dromara.x-file-storage x-file-storage-spring 2.3.0 ``` -------------------------------- ### Configure Mongo GridFS Storage Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for using Mongo GridFS as a file storage backend. Requires connection string, database, and bucket name. The domain is used for accessing stored files. ```yaml mongo-grid-fs: - platform: mongo-grid-fs-1 # 存储平台标识 enable-storage: true # 启用存储 connection-string: mongodb://admin:123456@192.168.1.111:27017 database: abc # 数据库名称 bucket-name: ccd # GridFS 存储通名称 domain: https://abc.com/ # 访问域名,注意“/”结尾,主要用于配合你自己写的访问接口进行访问 base-path: hy/ # 基础路径 ``` -------------------------------- ### Add Huawei Cloud OBS Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet provides the Maven dependency for integrating with Huawei Cloud Object Storage Service (OBS). Ensure the version is compatible with your project requirements. ```xml com.huaweicloud esdk-obs-java 3.22.12 ``` -------------------------------- ### Azure Blob Storage Configuration (YAML) Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for Azure Blob Storage. Requires connection string or endpoint and container name. `domain` should match the endpoint. `base-path` is a prefix for files. ```yaml azure-blob: - platform: azure-blob-1 enable-storage: true connection-string: ?? end-point: ?? container-name: ?? domain: ?? base-path: hy/ ``` -------------------------------- ### Huawei Cloud OBS Configuration Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This YAML snippet configures the Huawei Cloud OBS platform within the x-file-storage framework. It includes essential parameters such as access keys, endpoint, bucket name, domain, and base path. Refer to the specified Java class for more details. ```yaml huawei-obs: - platform: huawei-obs-1 # 存储平台标识 enable-storage: true # 启用存储 access-key: ?? secret-key: ?? end-point: ?? bucket-name: ?? domain: ?? # 访问域名,注意“/”结尾,例如:http://abc.obs.com/ base-path: test/ # 基础路径 ``` -------------------------------- ### Upload File to a Specific Storage Platform Source: https://github.com/dromara/x-file-storage/blob/main/README.md This example shows how to upload a file to a designated storage platform, such as 'aliyun-oss-1', using the `FileStorageService`. This is useful when you need to manage uploads across different storage providers. ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.multipart.MultipartFile; import com.xuyao.plugin.core.service.FileStorageService; import com.xuyao.plugin.core.vo.FileInfo; @RestController public class FileDetailController { @Autowired private FileStorageService fileStorageService; /** * 上传文件到指定存储平台,成功返回文件信息 */ @PostMapping("/upload-platform") public FileInfo uploadPlatform(MultipartFile file) { return fileStorageService.of(file) .setPlatform("aliyun-oss-1") //使用指定的存储平台 .upload(); } } ``` -------------------------------- ### Volcano Engine TOS Maven Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This code snippet provides the Maven dependency for integrating with Volcano Engine's TOS (Total Object Storage). Include this in your project's pom.xml to utilize the TOS platform. ```xml com.volcengine ve-tos-java-sdk 2.8.8 ``` -------------------------------- ### Baidu BOS Configuration Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This YAML snippet outlines the configuration for Baidu BOS (Baidu Object Storage). Key parameters include access key, secret key, endpoint, bucket name, domain, and base path. The `platform` identifier should be unique. ```yaml baidu-bos: - platform: baidu-bos-1 # 存储平台标识 enable-storage: true # 启用存储 access-key: ?? secret-key: ?? end-point: ?? # 例如 abc.fsh.bcebos.com bucket-name: ?? domain: ?? # 访问域名,注意“/”结尾,例如:https://abc.fsh.bcebos.com/abc/ base-path: test/ # 基础路径 ``` -------------------------------- ### Retrieve File ACL using Huawei OBS Client Source: https://github.com/dromara/x-file-storage/blob/main/docs/acl.md This snippet demonstrates retrieving file ACLs by manually obtaining the storage platform's client. It uses Huawei OBS as an example, illustrating how to get the client, bucket name, and file key to call the getObjectAcl method. This approach is less common and requires platform-specific client handling. ```java FileInfo fileInfo = fileStorageService.getFileInfoByUrl("https://file.abc.com/test/a.jpg"); HuaweiObsFileStorage fileStorage = fileStorageService.getFileStorage(fileInfo.getPlatform()); ObsClient client = fileStorage.getClient(); AccessControlList acl = client.getObjectAcl(fileStorage.getBucketName(),fileStorage.getFileKey(fileInfo)); ``` -------------------------------- ### GET /multipart/list-parts Source: https://context7.com/dromara/x-file-storage/llms.txt Lists all the parts that have been uploaded for a specific multipart upload. Useful for checking upload status or resuming interrupted uploads. ```APIDOC ## GET /multipart/list-parts ### Description Lists all uploaded parts for a given multipart upload. Allows specifying a maximum number of parts to return. ### Method GET ### Endpoint /multipart/list-parts ### Parameters #### Query Parameters - **uploadId** (string) - Required - The unique identifier for the multipart upload. - **maxParts** (integer) - Optional - The maximum number of parts to list. Defaults to a system-defined value if not provided. #### Request Body None ### Response #### Success Response (200) - **List<FilePartInfo>** (array) - A list of objects, where each object represents an uploaded part. - **partNumber** (integer) - The number of the part. - **eTag** (string) - The entity tag of the part. - **size** (long) - The size of the part in bytes. - **lastModified** (string) - The date and time the part was last modified (ISO 8601 format). #### Response Example ```json [ { "partNumber": 1, "eTag": "etag-part-1", "size": 5242880, "lastModified": "2023-10-27T10:00:00Z" }, { "partNumber": 2, "eTag": "etag-part-2", "size": 5242880, "lastModified": "2023-10-27T10:05:00Z" } ] ``` ``` -------------------------------- ### Enable File Storage in Spring Boot Application Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Enables the X-File-Storage functionality by adding the `@EnableFileStorage` annotation to the Spring Boot application's main class. ```java @EnableFileStorage @SpringBootApplication public class SpringFileStorageTestApplication { public static void main(String[] args) { SpringApplication.run(SpringFileStorageTestApplication.class,args); } } ``` -------------------------------- ### Add Tencent Cloud COS Dependency Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md This snippet provides the Maven dependency for integrating with Tencent Cloud Object Storage (COS). Ensure the version is appropriate for your project. ```xml com.qcloud cos_api 5.6.137 ``` -------------------------------- ### Manage Files with FileInfo Objects (Java) Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Illustrates how to use FileInfo objects to manage files with X-File-Storage. This includes creating FileInfo instances manually or retrieving them by URL, and then performing operations like checking existence, downloading, and deleting files. This approach simplifies file operations when file metadata is readily available. ```java //手动构造文件信息,可用于其它操作 FileInfo fileInfo = new FileInfo() .setPlatform("huawei-obs-1") .setBasePath("test/") .setPath("aa/") .setFilename("image.png") .setThFilename("image.png.min.jpg"); //文件是否存在 boolean exists = fileStorageService.exists(fileInfo); //下载 byte[] bytes = fileStorageService.download(fileInfo).bytes(); //删除 fileStorageService.delete(fileInfo); //其它更多操作 ``` ```java //直接从数据库中获取 FileInfo 对象,更加方便执行其它操作 FileInfo fileInfo = fileStorageService.getFileInfoByUrl("https://abc.def.com/test/aa/image.png"); //文件是否存在 boolean exists = fileStorageService.exists("https://abc.def.com/test/aa/image.png"); //下载 byte[] bytes = fileStorageService.download("https://abc.def.com/test/aa/image.png").bytes(); //删除 fileStorageService.delete("https://abc.def.com/test/aa/image.png"); //其它更多操作 ``` -------------------------------- ### Manual Initialization of FileStorageService with FTP Source: https://github.com/dromara/x-file-storage/blob/main/docs/脱离SpringBoot单独使用.md This Java code demonstrates how to manually initialize the FileStorageService for standalone use. It involves creating a FileStorageProperties object, configuring an FtpConfig, and then building the FileStorageService. This allows for custom configurations of storage platforms, client factories, and aspects. ```java //配置文件定义存储平台 FileStorageProperties properties = new FileStorageProperties(); properties.setDefaultPlatform("ftp-1"); FtpConfig ftp = new FtpConfig(); ftp.setPlatform("ftp-1"); ftp.setHost("192.168.3.100"); ftp.setPort(2121); ftp.setUser("root"); ftp.setPassword("123456"); ftp.setDomain("ftp://192.168.3.100:2121/"); ftp.setBasePath("ftp/"); ftp.setStoragePath("/"); properties.setFtp(Collections.singletonList(ftp)); //创建,自定义存储平台、 Client 工厂、切面等功能都有对应的添加方法 FileStorageService service = FileStorageServiceBuilder.create(properties).useDefault().build(); //初始化完毕,开始上传吧 FileInfo fileInfo = service.of(new File("D:\\Desktop\\a.png")).upload(); System.out.println(fileInfo); ``` -------------------------------- ### Basic File Upload with FileStorageService Source: https://context7.com/dromara/x-file-storage/llms.txt Demonstrates a simple file upload using the FileStorageService in Java. This method handles the core upload functionality with minimal configuration, relying on default settings. It takes a MultipartFile as input and returns FileInfo. ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.multipart.MultipartFile; import com.dromara.x.file.storage.core.FileInfo; import com.dromara.x.file.storage.core.FileStorageService; @RestController public class FileController { @Autowired private FileStorageService fileStorageService; // 基础上传 - 一行代码完成文件上传 @PostMapping("/upload") public FileInfo upload(MultipartFile file) { return fileStorageService.of(file).upload(); } } ``` -------------------------------- ### POST /multipart/initiate Source: https://context7.com/dromara/x-file-storage/llms.txt Initiates a multipart upload process for a large file. This step prepares the system to receive file parts and returns an upload ID. ```APIDOC ## POST /multipart/initiate ### Description Initiates a multipart upload process for a large file, returning an upload ID for subsequent part uploads. Supports setting platform, path, filename, size, content type, object ID, and object type. ### Method POST ### Endpoint /multipart/initiate ### Parameters #### Query Parameters - **filename** (string) - Required - The original filename of the file to be uploaded. - **fileSize** (long) - Required - The total size of the file in bytes. #### Request Body None ### Response #### Success Response (200) - **FileInfo** (object) - An object containing information about the initiated upload, including the upload ID. - **uploadId** (string) - The unique identifier for this multipart upload. - **filename** (string) - The saved filename. - **size** (long) - The total size of the file. - **platform** (string) - The storage platform used. - **path** (string) - The storage path. - **contentType** (string) - The MIME type of the file. - **objectId** (string) - The associated object ID. - **objectType** (string) - The type of the associated object. #### Response Example ```json { "uploadId": "some-upload-id", "filename": "large_video.mp4", "size": 1073741824, "platform": "aliyun-oss-1", "path": "large-files/", "contentType": "application/octet-stream", "objectId": "video-001", "objectType": "video" } ``` ``` -------------------------------- ### Add x-file-storage-solon Dependency (Maven) Source: https://github.com/dromara/x-file-storage/blob/main/docs/在Solon中使用.md This snippet shows how to add the x-file-storage-solon core dependency to your project using Maven. This is required for Solon integration. Ensure you use the correct version compatible with your Solon setup. ```xml org.dromara.x-file-storage x-file-storage-solon 2.3.0 ``` -------------------------------- ### Configure Spring Boot MongoDB for MongoGridFS Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Optional Spring Boot configuration to prevent potential MongoDB service initialization errors when using MongoGridFS, even if not directly using Spring Data MongoDB for other purposes. ```yaml spring: data: mongodb: uri: mongodb://admin:123456@192.168.1.111:27017 ``` -------------------------------- ### Standalone File Storage Service Usage in Java Source: https://context7.com/dromara/x-file-storage/llms.txt This Java code demonstrates how to use the X-File-Storage library without relying on the Spring Boot framework. It shows the process of configuring storage properties, building the FileStorageService, and performing basic file operations like upload, download, and delete. ```java package com.example.standalone; import org.dromara.x.file.storage.core.FileInfo; import org.dromara.x.file.storage.core.FileStorageService; import org.dromara.x.file.storage.core.FileStorageServiceBuilder; import org.dromara.x.file.storage.core.FileStorageProperties; import org.dromara.x.file.storage.core.platform.AliyunOssFileStorageConfig; import java.io.File; /** * 脱离 Spring Boot 单独使用示例 */ public class StandaloneExample { public static void main(String[] args) { // 1. 创建配置 FileStorageProperties properties = new FileStorageProperties(); properties.setDefaultPlatform("aliyun-oss-1"); // 配置阿里云 OSS AliyunOssFileStorageConfig aliyunConfig = new AliyunOssFileStorageConfig(); aliyunConfig.setPlatform("aliyun-oss-1"); aliyunConfig.setEnableStorage(true); aliyunConfig.setAccessKey("YOUR_ACCESS_KEY"); aliyunConfig.setSecretKey("YOUR_SECRET_KEY"); aliyunConfig.setEndPoint("oss-cn-shanghai.aliyuncs.com"); aliyunConfig.setBucketName("my-bucket"); aliyunConfig.setDomain("https://my-bucket.oss-cn-shanghai.aliyuncs.com/"); aliyunConfig.setBasePath("files/"); properties.setAliyunOss(java.util.Collections.singletonList(aliyunConfig)); // 2. 构建 FileStorageService FileStorageService fileStorageService = new FileStorageServiceBuilder() .setProperties(properties) .build(); // 3. 使用文件存储服务 File file = new File("/path/to/your/file.jpg"); // 上传文件 FileInfo fileInfo = fileStorageService.of(file) .setPath("uploads/") .upload(); System.out.println("上传成功! URL: " + fileInfo.getUrl()); // 下载文件 byte[] bytes = fileStorageService.download(fileInfo).bytes(); System.out.println("下载成功! 大小: " + bytes.length + " bytes"); // 删除文件 boolean deleted = fileStorageService.delete(fileInfo); System.out.println("删除" + (deleted ? "成功" : "失败")); } } ``` -------------------------------- ### Advanced File Upload with Configuration Options Source: https://github.com/dromara/x-file-storage/blob/main/README.md This example shows how to upload a file with additional configurations like setting the path, filename, object ID, object type, and custom attributes. It utilizes the `FileStorageService` and returns the file's URL upon successful upload. Image processing is handled by the thumbnailator library. ```java import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.multipart.MultipartFile; import com.xuyao.plugin.core.service.FileStorageService; import com.xuyao.plugin.core.vo.FileInfo; @RestController public class FileDetailController { @Autowired private FileStorageService fileStorageService; /** * 上传文件,成功返回文件 url */ @PostMapping("/upload2") public String upload2(MultipartFile file) { FileInfo fileInfo = fileStorageService.of(file) .setPath("upload/") //保存到相对路径下,为了方便管理,不需要可以不写 .setSaveFilename("image.jpg") //设置保存的文件名,不需要可以不写,会随机生成 .setObjectId("0") //关联对象id,为了方便管理,不需要可以不写 .setObjectType("0") //关联对象类型,为了方便管理,不需要可以不写 .putAttr("role","admin") //保存一些属性,可以在切面、保存上传记录、自定义存储平台等地方获取使用,不需要可以不写 .upload(); //将文件上传到对应地方 return fileInfo == null ? "上传失败!" : fileInfo.getUrl(); } } ``` -------------------------------- ### Maven Dependency for X-File-Storage and AliCloud OSS Source: https://github.com/dromara/x-file-storage/blob/main/README.md This snippet shows how to include the X-File-Storage core library and the Alibaba Cloud OSS SDK in your Maven project. Ensure you use the correct versions for compatibility. The OSS SDK is required if you plan to use Alibaba Cloud OSS as one of your storage backends. ```xml org.dromara.x-file-storage x-file-storage-spring 2.3.0 com.aliyun.oss aliyun-sdk-oss 3.16.1 ``` -------------------------------- ### Amazon S3 Configuration (YAML) Source: https://github.com/dromara/x-file-storage/blob/main/docs/快速入门.md Configuration for Amazon S3 storage. Requires access key, secret key, region, and bucket name. The endpoint and domain can be specified or derived. `base-path` defines a prefix for stored files. ```yaml amazon-s3-v2: - platform: amazon-s3-1 enable-storage: true access-key: ?? secret-key: ?? region: ap-east-1 end-point: https://s3.ap-east-1.amazonaws.com/ bucket-name: abcd domain: https://abcd.s3.ap-east-1.amazonaws.com/ base-path: s3/ ``` -------------------------------- ### Java Multipart Upload Controller for X-File-Storage Source: https://context7.com/dromara/x-file-storage/llms.txt This Java controller demonstrates how to handle multipart file uploads, including initiation, uploading individual parts, completing the upload, aborting, and listing uploaded parts. It relies on the FileStorageService for the underlying storage operations. It supports breakpoint resume by managing upload IDs and part numbers. ```java import org.springframework.web.bind.annotation.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.List; @RestController public class MultipartUploadController { @Autowired private FileStorageService fileStorageService; // 初始化分片上传 @PostMapping("/multipart/initiate") public FileInfo initiateMultipartUpload(@RequestParam String filename, @RequestParam Long fileSize) { return fileStorageService.initiateMultipartUpload() .setPlatform("aliyun-oss-1") .setPath("large-files/") .setSaveFilename(filename) .setSize(fileSize) .setContentType("application/octet-stream") .setObjectId("video-001") .setObjectType("video") .initiate(); } // 上传单个分片 @PostMapping("/multipart/upload-part") public FilePartInfo uploadPart(@RequestParam String uploadId, @RequestParam Integer partNumber, @RequestParam MultipartFile partFile) { // 从数据库获取 FileInfo (包含 uploadId) FileInfo fileInfo = getFileInfoFromDatabase(uploadId); return fileStorageService.uploadPart(fileInfo, partNumber, partFile) .uploadPart(); } // 完成分片上传 @PostMapping("/multipart/complete") public FileInfo completeMultipartUpload(@RequestParam String uploadId) { FileInfo fileInfo = getFileInfoFromDatabase(uploadId); return fileStorageService.completeMultipartUpload(fileInfo) .complete(); } // 取消分片上传 @PostMapping("/multipart/abort") public boolean abortMultipartUpload(@RequestParam String uploadId) { FileInfo fileInfo = getFileInfoFromDatabase(uploadId); return fileStorageService.abortMultipartUpload(fileInfo) .abort(); } // 列出已上传的分片 @GetMapping("/multipart/list-parts") public List listParts(@RequestParam String uploadId) { FileInfo fileInfo = getFileInfoFromDatabase(uploadId); return fileStorageService.listParts(fileInfo) .setMaxParts(1000) .listParts(); } // 完整的分片上传示例 @PostMapping("/multipart/upload-large-file") public FileInfo uploadLargeFile(@RequestParam MultipartFile file) throws IOException { // 1. 初始化 FileInfo fileInfo = fileStorageService.initiateMultipartUpload() .setPlatform("aliyun-oss-1") .setPath("videos/") .setSaveFilename(file.getOriginalFilename()) .setSize(file.getSize()) .initiate(); // 2. 分片上传 (每片 5MB) int partSize = 5 * 1024 * 1024; byte[] buffer = new byte[partSize]; InputStream inputStream = file.getInputStream(); int partNumber = 1; int bytesRead; while ((bytesRead = inputStream.read(buffer)) > 0) { byte[] partData = Arrays.copyOf(buffer, bytesRead); fileStorageService.uploadPart(fileInfo, partNumber, partData) .uploadPart(); partNumber++; } inputStream.close(); // 3. 完成上传 return fileStorageService.completeMultipartUpload(fileInfo) .complete(); } private FileInfo getFileInfoFromDatabase(String uploadId) { // 从数据库查询 FileInfo (需要实现 FileRecorder) return fileStorageService.getFileInfoByUrl(uploadId); } } ```