java对接IPFS系统-以nft.storage为列

引言

之前我们已经说过了、NFT.Storage是一个基于IPFS的分布式存储服务,专门用于存储和管理非同质化代币(NFT)相关的数据和资产。它是由Protocol Labs和Pinata共同推出的服务。今天我们基于nft.storage为列、使用java对接打通这个ipfs分布式存储系统。

展开之前也可以看之前对IFPS的基本介绍《IPFS分布式存储系统》、相信对你更加有帮助。

一、nft.storage操作(必读)

注意:要使用科学上网

  • 1.1首先我们打开之后官网是这样子的
    在这里插入图片描述
  • 1.2创建项目私钥,到时候对接api需要
    官方指引:https://app.nft.storage/v1/docs/intro
    在这里插入图片描述
  • 1.3注意api对接官方有QPS限制要求,
    在这里插入图片描述

二、java对接

首先引入核心依赖pom

<dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>jsr305</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>${okhttp-version}</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>logging-interceptor</artifactId>
            <version>${okhttp-version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>okhttp</artifactId>
                    <groupId>com.squareup.okhttp3</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson-version}</version>
        </dependency>
        <dependency>
            <groupId>io.gsonfire</groupId>
            <artifactId>gson-fire</artifactId>
            <version>${gson-fire-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3-version}</version>
        </dependency>
        <dependency>
            <groupId>org.threeten</groupId>
            <artifactId>threetenbp</artifactId>
            <version>${threetenbp-version}</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>${javax-annotation-version}</version>
            <scope>provided</scope>
        </dependency>
        <!-- test dependencies -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit-version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <properties>
        <java.version>1.7</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <gson-fire-version>1.8.5</gson-fire-version>
        <swagger-core-version>1.6.2</swagger-core-version>
        <okhttp-version>3.8.1</okhttp-version>
        <gson-version>2.8.6</gson-version>
        <commons-lang3-version>3.11</commons-lang3-version>
        <threetenbp-version>1.5.0</threetenbp-version>
        <javax-annotation-version>1.3.2</javax-annotation-version>
        <junit-version>4.13.1</junit-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

核心api

 

import com.google.gson.reflect.TypeToken;
import com.nft.server.file.nft.*;
import com.nft.server.file.nft.model.DeleteResponse;
import com.nft.server.file.nft.model.GetResponse;
import com.nft.server.file.nft.model.ListResponse;
import com.nft.server.file.nft.model.UploadResponse;
import org.springframework.stereotype.Component;
import org.threeten.bp.OffsetDateTime;

import java.io.File;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class NftStorageApi {
    private ApiClient localVarApiClient;

    public NftStorageApi() {
        this(Configuration.getDefaultApiClient());
    }

    public NftStorageApi(ApiClient apiClient) {
        this.localVarApiClient = apiClient;
    }

    public ApiClient getApiClient() {
        return localVarApiClient;
    }

    public void setApiClient(ApiClient apiClient) {
        this.localVarApiClient = apiClient;
    }

    /**
     * Build call for delete
     * @param cid CID for the NFT (required)
     * @param _callback Callback for upload/download progress
     * @return Call to execute
     * @throws ApiException If fail to serialize the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call deleteCall(String cid, final ApiCallback _callback) throws ApiException {
        Object localVarPostBody = null;

        // create path and map variables
        String localVarPath = "/{cid}"
            .replaceAll("\\{" + "cid" + "\\}", localVarApiClient.escapeString(cid.toString()));

        List<Pair> localVarQueryParams = new ArrayList<Pair>();
        List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
        Map<String, String> localVarHeaderParams = new HashMap<String, String>();
        Map<String, String> localVarCookieParams = new HashMap<String, String>();
        Map<String, Object> localVarFormParams = new HashMap<String, Object>();

        final String[] localVarAccepts = {
            "application/json"
        };
        final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) {
            localVarHeaderParams.put("Accept", localVarAccept);
        }

        final String[] localVarContentTypes = {
            
        };
        final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        String[] localVarAuthNames = new String[] { "bearerAuth" };
        return localVarApiClient.buildCall(localVarPath, "DELETE", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback);
    }

    @SuppressWarnings("rawtypes")
    private okhttp3.Call deleteValidateBeforeCall(String cid, final ApiCallback _callback) throws ApiException {
        
        // verify the required parameter 'cid' is set
        if (cid == null) {
            throw new ApiException("Missing the required parameter 'cid' when calling delete(Async)");
        }
        

        okhttp3.Call localVarCall = deleteCall(cid, _callback);
        return localVarCall;

    }

    /**
     * Stop storing the content with the passed CID
     * Stop storing the content with the passed CID on nft.storage. - Unpin the item from the underlying IPFS pinning service. - Cease renewals for expired Filecoin deals involving the CID.    ⚠️ This does not remove the content from the network.  - Does not terminate any established Filecoin deal. - Does not remove the content from other IPFS nodes in the network that already cached or pinned the CID.    Note: the content will remain available if another user has stored the CID with nft.storage. 
     * @param cid CID for the NFT (required)
     * @return DeleteResponse
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public DeleteResponse delete(String cid) throws ApiException {
        ApiResponse<DeleteResponse> localVarResp = deleteWithHttpInfo(cid);
        return localVarResp.getData();
    }

    /**
     * Stop storing the content with the passed CID
     * Stop storing the content with the passed CID on nft.storage. - Unpin the item from the underlying IPFS pinning service. - Cease renewals for expired Filecoin deals involving the CID.    ⚠️ This does not remove the content from the network.  - Does not terminate any established Filecoin deal. - Does not remove the content from other IPFS nodes in the network that already cached or pinned the CID.    Note: the content will remain available if another user has stored the CID with nft.storage. 
     * @param cid CID for the NFT (required)
     * @return ApiResponse&lt;DeleteResponse&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public ApiResponse<DeleteResponse> deleteWithHttpInfo(String cid) throws ApiException {
        okhttp3.Call localVarCall = deleteValidateBeforeCall(cid, null);
        Type localVarReturnType = new TypeToken<DeleteResponse>(){}.getType();
        return localVarApiClient.execute(localVarCall, localVarReturnType);
    }

    /**
     * Stop storing the content with the passed CID (asynchronously)
     * Stop storing the content with the passed CID on nft.storage. - Unpin the item from the underlying IPFS pinning service. - Cease renewals for expired Filecoin deals involving the CID.    ⚠️ This does not remove the content from the network.  - Does not terminate any established Filecoin deal. - Does not remove the content from other IPFS nodes in the network that already cached or pinned the CID.    Note: the content will remain available if another user has stored the CID with nft.storage. 
     * @param cid CID for the NFT (required)
     * @param _callback The callback to be executed when the API call finishes
     * @return The request call
     * @throws ApiException If fail to process the API call, e.g. serializing the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call deleteAsync(String cid, final ApiCallback<DeleteResponse> _callback) throws ApiException {

        okhttp3.Call localVarCall = deleteValidateBeforeCall(cid, _callback);
        Type localVarReturnType = new TypeToken<DeleteResponse>(){}.getType();
        localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
        return localVarCall;
    }
    /**
     * Build call for list
     * @param before Return results created before provided timestamp (optional)
     * @param limit Max records to return (optional, default to 10)
     * @param _callback Callback for upload/download progress
     * @return Call to execute
     * @throws ApiException If fail to serialize the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call listCall(OffsetDateTime before, Integer limit, final ApiCallback _callback) throws ApiException {
        Object localVarPostBody = null;

        // create path and map variables
        String localVarPath = "/";

        List<Pair> localVarQueryParams = new ArrayList<Pair>();
        List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
        Map<String, String> localVarHeaderParams = new HashMap<String, String>();
        Map<String, String> localVarCookieParams = new HashMap<String, String>();
        Map<String, Object> localVarFormParams = new HashMap<String, Object>();

        if (before != null) {
            localVarQueryParams.addAll(localVarApiClient.parameterToPair("before", before));
        }

        if (limit != null) {
            localVarQueryParams.addAll(localVarApiClient.parameterToPair("limit", limit));
        }

        final String[] localVarAccepts = {
            "application/json"
        };
        final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) {
            localVarHeaderParams.put("Accept", localVarAccept);
        }

        final String[] localVarContentTypes = {
            
        };
        final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        String[] localVarAuthNames = new String[] { "bearerAuth" };
        return localVarApiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback);
    }

    @SuppressWarnings("rawtypes")
    private okhttp3.Call listValidateBeforeCall(OffsetDateTime before, Integer limit, final ApiCallback _callback) throws ApiException {
        

        okhttp3.Call localVarCall = listCall(before, limit, _callback);
        return localVarCall;

    }

    /**
     * List all stored files
     * 
     * @param before Return results created before provided timestamp (optional)
     * @param limit Max records to return (optional, default to 10)
     * @return ListResponse
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public ListResponse list(OffsetDateTime before, Integer limit) throws ApiException {
        ApiResponse<ListResponse> localVarResp = listWithHttpInfo(before, limit);
        return localVarResp.getData();
    }

    /**
     * List all stored files
     * 
     * @param before Return results created before provided timestamp (optional)
     * @param limit Max records to return (optional, default to 10)
     * @return ApiResponse&lt;ListResponse&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public ApiResponse<ListResponse> listWithHttpInfo(OffsetDateTime before, Integer limit) throws ApiException {
        okhttp3.Call localVarCall = listValidateBeforeCall(before, limit, null);
        Type localVarReturnType = new TypeToken<ListResponse>(){}.getType();
        return localVarApiClient.execute(localVarCall, localVarReturnType);
    }

    /**
     * List all stored files (asynchronously)
     * 
     * @param before Return results created before provided timestamp (optional)
     * @param limit Max records to return (optional, default to 10)
     * @param _callback The callback to be executed when the API call finishes
     * @return The request call
     * @throws ApiException If fail to process the API call, e.g. serializing the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call listAsync(OffsetDateTime before, Integer limit, final ApiCallback<ListResponse> _callback) throws ApiException {

        okhttp3.Call localVarCall = listValidateBeforeCall(before, limit, _callback);
        Type localVarReturnType = new TypeToken<ListResponse>(){}.getType();
        localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
        return localVarCall;
    }
    /**
     * Build call for status
     * @param cid CID for the NFT (required)
     * @param _callback Callback for upload/download progress
     * @return Call to execute
     * @throws ApiException If fail to serialize the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call statusCall(String cid, final ApiCallback _callback) throws ApiException {
        Object localVarPostBody = null;

        // create path and map variables
        String localVarPath = "/{cid}"
            .replaceAll("\\{" + "cid" + "\\}", localVarApiClient.escapeString(cid.toString()));

        List<Pair> localVarQueryParams = new ArrayList<Pair>();
        List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
        Map<String, String> localVarHeaderParams = new HashMap<String, String>();
        Map<String, String> localVarCookieParams = new HashMap<String, String>();
        Map<String, Object> localVarFormParams = new HashMap<String, Object>();

        final String[] localVarAccepts = {
            "application/json"
        };
        final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) {
            localVarHeaderParams.put("Accept", localVarAccept);
        }

        final String[] localVarContentTypes = {
            
        };
        final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        String[] localVarAuthNames = new String[] { "bearerAuth" };
        return localVarApiClient.buildCall(localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback);
    }

    @SuppressWarnings("rawtypes")
    private okhttp3.Call statusValidateBeforeCall(String cid, final ApiCallback _callback) throws ApiException {
        
        // verify the required parameter 'cid' is set
        if (cid == null) {
            throw new ApiException("Missing the required parameter 'cid' when calling status(Async)");
        }
        

        okhttp3.Call localVarCall = statusCall(cid, _callback);
        return localVarCall;

    }

    /**
     * Get information for the stored file CID
     * Includes the IPFS pinning state and the Filecoin deal state.
     * @param cid CID for the NFT (required)
     * @return GetResponse
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public GetResponse status(String cid) throws ApiException {
        ApiResponse<GetResponse> localVarResp = statusWithHttpInfo(cid);
        return localVarResp.getData();
    }

    /**
     * Get information for the stored file CID
     * Includes the IPFS pinning state and the Filecoin deal state.
     * @param cid CID for the NFT (required)
     * @return ApiResponse&lt;GetResponse&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public ApiResponse<GetResponse> statusWithHttpInfo(String cid) throws ApiException {
        okhttp3.Call localVarCall = statusValidateBeforeCall(cid, null);
        Type localVarReturnType = new TypeToken<GetResponse>(){}.getType();
        return localVarApiClient.execute(localVarCall, localVarReturnType);
    }

    /**
     * Get information for the stored file CID (asynchronously)
     * Includes the IPFS pinning state and the Filecoin deal state.
     * @param cid CID for the NFT (required)
     * @param _callback The callback to be executed when the API call finishes
     * @return The request call
     * @throws ApiException If fail to process the API call, e.g. serializing the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call statusAsync(String cid, final ApiCallback<GetResponse> _callback) throws ApiException {

        okhttp3.Call localVarCall = statusValidateBeforeCall(cid, _callback);
        Type localVarReturnType = new TypeToken<GetResponse>(){}.getType();
        localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
        return localVarCall;
    }
    /**
     * Build call for store
     * @param body  (required)
     * @param _callback Callback for upload/download progress
     * @return Call to execute
     * @throws ApiException If fail to serialize the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call storeCall(File body, final ApiCallback _callback) throws ApiException {
        Object localVarPostBody = body;

        // create path and map variables
        String localVarPath = "/upload";

        List<Pair> localVarQueryParams = new ArrayList<Pair>();
        List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
        Map<String, String> localVarHeaderParams = new HashMap<String, String>();
        Map<String, String> localVarCookieParams = new HashMap<String, String>();
        Map<String, Object> localVarFormParams = new HashMap<String, Object>();

        final String[] localVarAccepts = {
            "application/json"
        };
        final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
        if (localVarAccept != null) {
            localVarHeaderParams.put("Accept", localVarAccept);
        }

        final String[] localVarContentTypes = {
            "image/png", "application/octet-stream", "multipart/form-data"
        };
        final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
        localVarHeaderParams.put("Content-Type", localVarContentType);

        String[] localVarAuthNames = new String[] { "bearerAuth" };
        return localVarApiClient.buildCall(localVarPath, "POST", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback);
    }

    @SuppressWarnings("rawtypes")
    private okhttp3.Call storeValidateBeforeCall(File body, final ApiCallback _callback) throws ApiException {
        
        // verify the required parameter 'body' is set
        if (body == null) {
            throw new ApiException("Missing the required parameter 'body' when calling store(Async)");
        }
        

        okhttp3.Call localVarCall = storeCall(body, _callback);
        return localVarCall;

    }

    /**
     * Store a file
     * Store a file with nft.storage.  - Submit a HTTP &#x60;POST&#x60; request passing the file data in the request body. - To store multiple files in a directory, submit a &#x60;multipart/form-data&#x60; HTTP &#x60;POST&#x60; request.  Use the &#x60;Content-Disposition&#x60; header for each part to specify a filename. 
     * @param body  (required)
     * @return UploadResponse
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public UploadResponse store(File body) throws ApiException {
        ApiResponse<UploadResponse> localVarResp = storeWithHttpInfo(body);
        return localVarResp.getData();
    }

    /**
     * Store a file
     * Store a file with nft.storage.  - Submit a HTTP &#x60;POST&#x60; request passing the file data in the request body. - To store multiple files in a directory, submit a &#x60;multipart/form-data&#x60; HTTP &#x60;POST&#x60; request.  Use the &#x60;Content-Disposition&#x60; header for each part to specify a filename. 
     * @param body  (required)
     * @return ApiResponse&lt;UploadResponse&gt;
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public ApiResponse<UploadResponse> storeWithHttpInfo(File body) throws ApiException {
        okhttp3.Call localVarCall = storeValidateBeforeCall(body, null);
        Type localVarReturnType = new TypeToken<UploadResponse>(){}.getType();
        return localVarApiClient.execute(localVarCall, localVarReturnType);
    }

    /**
     * Store a file (asynchronously)
     * Store a file with nft.storage.  - Submit a HTTP &#x60;POST&#x60; request passing the file data in the request body. - To store multiple files in a directory, submit a &#x60;multipart/form-data&#x60; HTTP &#x60;POST&#x60; request.  Use the &#x60;Content-Disposition&#x60; header for each part to specify a filename. 
     * @param body  (required)
     * @param _callback The callback to be executed when the API call finishes
     * @return The request call
     * @throws ApiException If fail to process the API call, e.g. serializing the request body object
     * @http.response.details
     <table summary="Response Details" border="1">
        <tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
        <tr><td> 200 </td><td> OK </td><td>  -  </td></tr>
        <tr><td> 401 </td><td> Unauthorized </td><td>  -  </td></tr>
        <tr><td> 403 </td><td> Forbidden </td><td>  -  </td></tr>
        <tr><td> 5XX </td><td> Internal Server Error </td><td>  -  </td></tr>
     </table>
     */
    public okhttp3.Call storeAsync(File body, final ApiCallback<UploadResponse> _callback) throws ApiException {

        okhttp3.Call localVarCall = storeValidateBeforeCall(body, _callback);
        Type localVarReturnType = new TypeToken<UploadResponse>(){}.getType();
        localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
        return localVarCall;
    }
}

package com.nft.server.file.nft.apikey;

import com.nft.server.base.redis.RedisTemplateService;
import com.nft.server.system.po.SysSettingPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.nft.common.base.constant.SysSettingConstant.NFT_API_KEY_LIST;

/**
 *
 *
 * @author nftStorage的apiKey
 * @version
 */
@Component
public class ApiKeyArray {
    private static final String[] apiKeyArray = {
            "----------------key------"
          
    };
    private int currentIndex = 0;

    @Autowired
    private RedisTemplateService redisTemplateService;


    // 轮询获取apiKey
    public  String getApiKey() {
        List<Object> apiKeyArray = redisTemplateService.lGet(NFT_API_KEY_LIST, 0, -1);
        String value = (String) apiKeyArray.get(currentIndex);
        currentIndex = (currentIndex + 1) % apiKeyArray.size();
        return value;
    }


}

上传文件

 
            ApiClient apiClient = nftStorageApi.getApiClient();
            // Configure HTTP bearer authorization: bearerAuth
            HttpBearerAuth bearerAuth = (HttpBearerAuth) apiClient.getAuthentication("bearerAuth");
            // 获取apikey
            bearerAuth.setBearerToken(apiKeyArray.getApiKey());
            //传入文件流
            File body = convertMultiPartToFile(file);
            UploadResponse result = nftStorageApi.store(body);
            if (result.getOk()) {
                NFT nft = result.getValue();
                String cid = nft.getCid();
                url = getNftStorageFileUrl(cid);
                 
                return url;
            }

以上是核心代码,依赖代码我上次到资源里面,免费下载

https://download.csdn.net/download/qq_38420688/89161667

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/553927.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

成为程序员后的领悟与展望-ApiHug

&#x1f917; ApiHug {Postman|Swagger|Api...} 快↑ 准√ 省↓ GitHub - apihug/apihug.com: All abou the Apihug apihug.com: 有爱&#xff0c;有温度&#xff0c;有质量&#xff0c;有信任ApiHug - API design Copilot - IntelliJ IDEs Plugin | Marketplace 选择一个…

【代码】Python3|Requests 库怎么继承 Selenium 的 Headers (2024,Chrome)

本文使用的版本&#xff1a; Chrome 124Python 12Selenium 4.19.0 版本过旧可能会出现问题&#xff0c;但只要别差异太大&#xff0c;就可以看本文&#xff0c;因为本文对新老版本都有讲解。 文章目录 1 难点解析和具体思路2 注意事项2.1 PDF 资源获取时注意事项2.2 Capabiliti…

关于老iPad 能够重新使用经过的一些列折腾

背景 搞了一台IPad air一代给家里老人看戏曲或者电视用,芯片是A7处理器,目前IOS系统是IOS12,也就是能支持的最后一个版本。并且可能是之前刷机问题,IPad基带丢失,显示无法连接激活服务器,无法进入系统。 本人没有MAC设备,没有相关越狱经验,没有黑苹果经验,一切都是从头…

制冷铜管焊接介绍

铜管是制冷装置的重要原材料&#xff0c;它主要有两种用途&#xff1a;①制作换热器。②制作连接管道和管件。常用的焊料类型有铜磷焊料、银铜焊料、铜锌焊料等。在焊接时要根据管道材料的特点&#xff0c;正确的选择焊料及熟练的操作&#xff0c;以确保焊接的质量。 1.1对同类…

基于springboot实现图书进销存管理系统项目【项目源码+论文说明】计算机毕业设计

基于springboot实现图书进销存管理系统演示 摘要 随着信息技术在管理上越来越深入而广泛的应用&#xff0c;管理信息系统的实施在技术上已逐步成熟。本文介绍了图书进销存管理系统的开发全过程。通过分析图书进销存管理系统管理的不足&#xff0c;创建了一个计算机管理图书进销…

QML QtObject轻量级非可视化元素

QtObject 理论1. 父指针形式代指子类2. 自定义组件中定义一些私有属性 理论 QtObject类型是一个非常轻量级且非可视元素&#xff0c;它只包含objectName属性&#xff0c;其本质上是QObject。 用途一般是两个&#xff1a; 父指针形式代指子类&#xff1b;自定义组件中定义一些…

Java基于SpringBoot+Vue的蜗牛兼职网系统的研究与实现

博主介绍&#xff1a;✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;…

Python零基础从小白打怪升级中~~~~~~~多线程

线程安全和锁 一、全局解释器锁 首先需要明确的一点是GIL并不是Python的特性&#xff0c;它是在实现Python解析器(CPython)时所引入的一个概念。 GIL全称global interpreter lock&#xff0c;全局解释器锁。 每个线程在执行的时候都需要先获取GIL&#xff0c;保证同一时刻只…

MySQL(2024.4.17)

目录 1. 什么是MySQL的MVCC机制&#xff1f; 2. 如何理解InnoDB的Next-Key Lock机制&#xff1f; 3. 快照读和当前读的区别&#xff1f; 4. 如何在SQL语句中触发当前读&#xff1f; 5. MySQL默认的隔离级别是什么&#xff1f; 6. 如何避免在使用当前读时可能出现的死锁问…

Docker部署metahuman-stream数字人系统

metahuman-stream是基于ernerf模型的流式数字人&#xff0c;实现音视频同步对话。 metahuman-stream xtts-streaming-server srs 部署 srs # rtmpserver docker run -it -d \ -p 1935:1935 -p 1985:1985 -p 8080:8080 -p 8000:8000/udp -p 10080:10080/udp \ --name srs \ reg…

Postgresql源码(126)TupleStore使用场景与原理分析

相关 《Postgresql源码&#xff08;125&#xff09;游标恢复执行的原理分析》 《Postgresql游标使用介绍&#xff08;cursor&#xff09;》 总结 开源PG中使用tuple store来缓存tuple集&#xff0c;默认使用work_mem空间存放&#xff0c;超过可以落盘。在PL的returns setof场景…

基于51单片机的步进电机调速系统设计

基于51单片机的步进电机调速系统 &#xff08;仿真&#xff0b;程序&#xff0b;原理图&#xff0b;设计报告&#xff09; 功能介绍 具体功能&#xff1a; 1.按键可以控制电机正、反转&#xff0c;加、减速&#xff0c;停止&#xff1b; 2.一位7段数码管实时显示档位&#xf…

6个免费的伪原创工具,轻松生成原创文章

如今&#xff0c;内容创作已经成为许多人关注的焦点。然而&#xff0c;随之而来的是创作压力和时间成本的增加。为了解决这些问题&#xff0c;越来越多的人开始寻找一些伪原创工具来帮助他们生成原创文章&#xff0c;其中免费的伪原创工具成为了热门选择之一。这些免费的伪原创…

建都寿春的袁术兴亡史

三国(220年-280年)是中国历史上位于汉朝之后&#xff0c;晋朝之前的一段历史时期。这一个时期&#xff0c;先后出现了曹魏、蜀汉、东吴三个主要政权。袁术的地盘很小&#xff0c;为了在三国时期能够立足&#xff1f; 事实上&#xff0c;袁术巅峰时期的地盘并不小&#xff0c;而…

类和对象中-运算符重载

在C中&#xff0c;有些成员函数如果我们不去显示定义&#xff0c;编译器会自动生成 会自动生成的特殊函数&#xff1a; 他们不能定义为全局函数&#xff0c;必须是类成员员函数&#xff08;特别是拷贝赋值重载&#xff09; 下面介绍默认生成函数的作用&#xff0c;特点 构造 …

工作流JBPM流程图说明

文章目录 5☃️ 相关概念6 ☃️流程图说明6.0 ❄️❄️快速上手6.1 ❄️❄️活动Activity / 节点Node6.1.1 start 开始活动6.1.2 end 结束活动6.1.3 task 任务活动6.1.4 decision 判断活动6.1.5 fork/join 分支/聚合活动 6.2 ❄️❄️流转 Transition / 连线 &#xff08;单向箭…

Python相关性分析

分析连续变量之间线性相关程度的强弱&#xff0c;并用适当的统计指标表示出来的过程称为相关分析。 可以直接绘制散点图&#xff0c;或者绘制散点图矩阵&#xff0c;或者计算相关系数来进行相关分析。 相关系数的计算如下所示&#xff1a; 示例数据&#xff1a; 计算百合酱蒸…

云服务器安装Mysql、MariaDB、Redis、tomcat、nginx

前置工作 进入根目录 cd / 都在/usr/local/src文件夹&#xff09; 上传压缩包 rz 压缩包 Mysql 1.下载并安装MySQL官方的 Yum Repository wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-release-el7-5.noarch.rpm yum…

lua 光速入门

文章目录 安装注释字符串变量逻辑运算条件判断循环函数Table (表)常用全局函数模块化 首先明确 lua 和 js Python一样是动态解释性语言&#xff0c;需要解释器执行。并且不同于 Python 的强类型与 js 的弱类型&#xff0c;它有点居中&#xff0c;倾向于强类型。 安装 下载解释…

美易官方:人民币国际支付占比升至近5%

随着全球金融市场的不断发展和数字化进程的加速&#xff0c;人民币的国际支付地位逐渐提升&#xff0c;成为备受瞩目的焦点。最近的数据显示&#xff0c;人民币在国际支付中的占比已经升至近5%&#xff0c;自11月以来已成为第四大交易货币。这一变化不仅反映了中国经济的崛起和…