Tag Archives: oss

About OSS file download and rename

Option 1

Add the response header through nginx, and add the parameter & amp; attname = filename after the download address

 location/{
           proxy_pass https://xxxx.oss-cn-shenzhen.aliyuncs.com;
           if ($query_string ~* ^(.*)attname=([^&]+)$) {
                add_header Content-Disposition "attachment;filename=$arg_attname";
           }
        }

Disadvantages: the download speed depends on the bandwidth of the server deployed by nginx, which affects the user experience
so there is a problem

Option 2

Get the source code of the file download address by viewing OSS:

@Override
    public URL generatePresignedUrl(String bucketName, String key, Date expiration) throws ClientException {
        return generatePresignedUrl(bucketName, key, expiration, HttpMethod.GET);
    }

    @Override
    public URL generatePresignedUrl(String bucketName, String key, Date expiration, HttpMethod method)
            throws ClientException {
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
        request.setExpiration(expiration);
        request.setMethod(method);

        return generatePresignedUrl(request);
    }

    @Override
    public URL generatePresignedUrl(GeneratePresignedUrlRequest request) throws ClientException {

        assertParameterNotNull(request, "request");

        if (request.getBucketName() == null) {
            throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("MustSetBucketName"));
        }
        ensureBucketNameValid(request.getBucketName());

        if (request.getExpiration() == null) {
            throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("MustSetExpiration"));
        }
        String url;

        if (serviceClient.getClientConfiguration().getSignatureVersion() != null && serviceClient.getClientConfiguration().getSignatureVersion() == SignVersion.V2) {
            url = SignV2Utils.buildSignedURL(request, credsProvider.getCredentials(), serviceClient.getClientConfiguration(), endpoint);
        } else {
            url = SignUtils.buildSignedURL(request, credsProvider.getCredentials(), serviceClient.getClientConfiguration(), endpoint);
        }

        try {
            return new URL(url);
        } catch (MalformedURLException e) {
            throw new ClientException(e);
        }
    }

It is found that the final call is generatepreseignedurl (generatepreseignedurlrequest request) method
so we study generatepreseignedurlrequest
and find a method to rewrite the response header

 /**
     * Sets the response headers to override.
     *
     * @param responseHeaders
     *            The response headers to override.
     */
    public void setResponseHeaders(ResponseHeaderOverrides responseHeaders) {
        this.responseHeaders = responseHeaders;
    }

Then test the following code

GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(BUCKETNAME, key);
request.setExpiration(new Date(System.currentTimeMillis() + 3600 * 1000 * 24 * 7));
request.setMethod(HttpMethod.GET);
ResponseHeaderOverrides header = new ResponseHeaderOverrides();
header.setContentDisposition("attachment;filename=" + fileName);
request.setResponseHeaders(header);
URL url = ossClient.generatePresignedUrl(request);

The returned URL contains more & amp; response content disposition = attachment% 3bfilename% 3dfilname
test links, which can be renamed