All Products
Search
Document Center

Object Storage Service:Query image information

Last Updated:Sep 23, 2024

Some images may contain Exchangeable Image File Format (EXIF) data that includes image attributes and shooting settings. The EXIF data of an image includes image information such as the compression ratio, orientation, horizontal resolution, and vertical resolution. If you want to obtain the EXIF data of an image, add the info parameter to the URL of the image.

Parameters

Action: info

The returned information is in the JSON format.

Note

You cannot use the info action together with other actions in an image processing request. For example, you cannot use image/resize,s_700,limit_0/info to resize an image and query information about the resized image at the same time.

Limits

You can use the info action to query information about only JPG, PNG, BMP, GIF, WebP, TIFF, and HEIC images.

Note

You cannot use the info action to query EXIF data of PNG images.

Operations

Obtain information about public-read or public-read-write images

You can obtain information about public-read or public-read-write images by adding image processing parameters to URLs directly.

  • Query information about an image that does not contain EXIF data

    https://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/info

    If you add the info parameter to the URL of an image that does not contain EXIF data, the response includes only the basic information about the image, such as the size, format, height, and width of the image.

    {
      "FileSize": {"value": "21839"},
      "Format": {"value": "jpg"},
      "FrameCount": {"value": "1"},
      "ImageHeight": {"value": "267"},
      "ImageWidth": {"value": "400"},
      "ResolutionUnit": {"value": "1"},
      "XResolution": {"value": "1/1"},
      "YResolution": {"value": "1/1"}
    }
  • Query information about an image that contains EXIF data

    https://image-demo.oss-cn-hangzhou.aliyuncs.com/f.jpg?x-oss-process=image/info

    If you add the info parameter to the URL of an image that contains EXIF data, the response includes the basic information about the image and the EXIF data of the image. For more information about EXIF data, see Exif Version 2.31.

    {
      "Compression": {"value": "6"},
      "DateTime": {"value": "2015:02:11 15:38:27"},
      "ExifTag": {"value": "2212"},
      "FileSize": {"value": "23471"},
      "Format": {"value": "jpg"},
      "GPSLatitude": {"value": "0deg "},
      "GPSLatitudeRef": {"value": "North"},
      "GPSLongitude": {"value": "0deg "},
      "GPSLongitudeRef": {"value": "East"},
      "GPSMapDatum": {"value": "WGS-84"},
      "GPSTag": {"value": "4292"},
      "GPSVersionID": {"value": "2 2 0 0"},
      "ImageHeight": {"value": "333"},
      "ImageWidth": {"value": "424"},
      "JPEGInterchangeFormat": {"value": "4518"},
      "JPEGInterchangeFormatLength": {"value": "3232"},
      "Orientation": {"value": "7"},
      "ResolutionUnit": {"value": "2"},
      "Software": {"value": "Microsoft Windows Photo Viewer 6.1.7600.16385"},
      "XResolution": {"value": "96/1"},
      "YResolution": {"value": "96/1"}}

Obtain information about private images

You can obtain information about private images by using Alibaba Cloud SDK or REST API.

Use Alibaba Cloud SDK

The following sample code provides examples on how to use SDKs for common programming languages to obtain information about images. For more information about how to obtain information about images by using SDKs for other programming languages, see SDK Reference.

Java

Java SDK version 3.17.4 or above is required.

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyuncs.exceptions.ClientException;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Demo{
    public static void main(String[] args) throws ClientException, ClientException {
        // yourEndpoint  Specify the endpoint of the region in which the bucket is located.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the endpoint of the region. For example, cn-hangzhou.
        String region = "cn-hangzhou";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the bucket.
        String bucketName = "examplebucket";
        // Specify the name of the image if it is stored in the root directory of the bucket. If it is stored elsewhere, you must specify the full path of the image. Example: exampledir/example.jpg. 
        String key = "example.jpg";

        // Create an OSSClient instance.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();

        try {
            // Create a request to obtain information about the image.
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
            getObjectRequest.setProcess("image/info");

            // Use getObject and input instructions via process parameters.
            OSSObject ossObject = ossClient.getObject(getObjectRequest);

            // Read and print information.
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = ossObject.getObjectContent().read(buffer)) != -1) {
                baos.write(buffer, 0, bytesRead);
            }
            String imageInfo = baos.toString("UTF-8");
            System.out.println("Image Info:");
            System.out.println(imageInfo);
        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            // Shut down OSSClient
            ossClient.shutdown();
        }
    }
}

PHP

PHP SDK version 2.7.0 or above is required.

<?php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;

try {
    // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
    $provider = new EnvironmentVariableCredentialsProvider(); 
    // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
    $endpoint = 'https://oss-cn-hangzhou.aliyuncs.com';
    // Specify the name of the bucket. Example: examplebucket.
    $bucket = 'examplebucket';
    // Specify the name of the image if it is stored in the root directory of the bucket. If it is stored elsewhere, you must specify the full path of the image. Example: exampledir/example.jpg. 
    $key = 'example.jpg'; 

    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,        
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        // Specify the Region ID.
        "region" => "cn-hangzhou"
    );
    $ossClient = new OssClient($config);
  // Create a request to obtain information about the image.
  $options[$ossClient::OSS_PROCESS] = "image/info";
  $result = $ossClient->getObject($bucket,$key,$options);
  var_dump($result);
} catch (OssException $e) {
  printf($e->getMessage() . "\n");
  return;
}

Go

Go SDK 3.0.2 or above is required.

package main

import (
	"fmt"
	"io"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Create an OSSClient instance.
	// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
	// Specify the Region ID. Example: cn-hangzhou.
	client, err := oss.New("https://oss-cn-hangzhou.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider), oss.AuthVersion(oss.AuthV4), oss.Region("cn-hangzhou"))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the name of the bucket. Example: examplebucket.
	bucketName := "examplebucket"

	bucket, err := client.Bucket(bucketName)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	// Specify the name of the image if it is stored in the root directory of the bucket. If it is stored elsewhere, you must specify the full path of the image. Example: exampledir/example.jpg.
	// Create a request to obtain information about the image by using oss.Process.
	body, err := bucket.GetObject("example.jpg", oss.Process("image/info"))
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}

	defer body.Close()

	data, err := io.ReadAll(body)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(-1)
	}
	fmt.Println("data:", string(data))
}

Use REST API

If your business requires a high level of customization, you can directly call RESTful APIs. To directly call an API, you must include the signature calculation in your code. For more information, see GetObject.

You can add IMG parameters or image style parameters to GetObject requests to process images.

GET /oss.jpg?x-oss-process=image/info HTTP/1.1
Host: oss-example.oss-cn-hangzhou.aliyuncs.com
Date: Fri, 28 Oct 2022 06:40:10 GMT
Authorization: OSS qn6q**************:77Dv****************

FAQ

How do I query information about a private image?

To query information about a private image, add the info parameter to a signed URL by using OSS SDKs and then use the URL to access the image.

  1. Generate a signed URL.

    The following sample code provides an example on how to use OSS SDK for Java to generate a signed URL:

    import com.aliyun.oss.*;
    import com.aliyun.oss.common.auth.*;
    import com.aliyun.oss.model.GeneratePresignedUrlRequest;
    import java.net.URL;
    import java.util.Date;
    
    public class Demo {
        public static void main(String[] args) throws Throwable {
            // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // For security purposes, we recommend that you do not save access credentials in the project code. In this example, access credentials are obtained from environment variables. Before you run the sample code, make sure that the environment variables are configured. 
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // Specify the name of the bucket. Example: examplebucket. 
            String bucketName = "examplebucket";
            // Specify the full path of the object. Do not include the bucket name in the full path. 
            String objectName = "exampleobject.jpg";
    
            // Create an OSSClient instance. 
            OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
    
            try {
                // Query information about the image object. 
                String style = "image/info";
                // Set the validity period of the signed URL to 10 minutes. 
                Date expiration = new Date(new Date().getTime() + 1000 * 60 * 10 );
                GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
                req.setExpiration(expiration);
                req.setProcess(style);
                URL signedUrl = ossClient.generatePresignedUrl(req);
                System.out.println(signedUrl);
            } 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();
                }
            }
        }
    }
  2. Use the signed URL to access the image from a browser to query image information.