By attaching a Resource Access Management (RAM) role to an Elastic Compute Service (ECS) instance outside an E-MapReduce (EMR) cluster, JindoFS SDK can retrieve temporary credentials from the instance metadata service automatically, eliminating the need to embed an AccessKey pair in your code or configuration.
How it works
When an ECS instance has an attached RAM role, the instance metadata service issues temporary credentials on demand. JindoFS SDK retrieves these credentials at runtime, removing the need to store an AccessKey pair anywhere in your environment.
Prerequisites
Before you begin, ensure that you have:
-
An ECS instance that is not part of an EMR cluster
-
The Hadoop ecosystem set up on the instance
-
JindoFS SDK for Java downloaded and available locally
Access OSS in password-free mode
To enable password-free access, create a RAM role with OSS permissions, attach it to your ECS instance, and verify that you can access OSS without providing credentials.
Before starting, remove any existing Jindo packages from your environment to prevent conflicts:
-
jboot.jar -
smartdata-aliyun-jfs-*.jar
If you use Spark, also remove all Jindo-related packages from /opt/apps/spark-current/jars/.
Step 1: Create an instance RAM role
-
Log on to the RAM consoleRAM console with your Alibaba Cloud account.
-
In the left-side navigation pane, choose Identities > Roles.
-
On the RAM Roles page, click Create Role.
-
In the Create RAM Role panel, select Alibaba Cloud Service for Trusted entity type.
-
Click Next.
-
Enter a name in the RAM Role Name field, then select Elastic Compute Service from the Select Trusted Service drop-down list.
-
Click OK.
Step 2: Grant permissions to the RAM role
-
In the left-side navigation pane of the RAM console, choose Identities > Roles.
-
Find the RAM role you created and click its name.
-
Click Precise Permission.
-
In the panel that appears, set Type to System Policy or Custom Policy, then enter the policy name. Use the minimum permissions required for your use case. For example,
AliyunOSSReadOnlyAccessgrants read-only access to OSS. For write access, select a policy that includes the specific OSS actions your application needs.If system policies don't meet your requirements, create a custom policy with only the OSS actions your application uses. For instructions, see the "(Optional) Create a custom authorization policy" section in Implement access control by using RAM.
-
Click OK.
Step 3: Attach the RAM role to an ECS instance
-
Log on to the ECS console.
-
In the left-side navigation pane, choose Instances & Images > Instances.
-
In the top navigation bar, select the region where your instance is located.
-
Find the target instance and choose
> Instance Settings > Attach/Detach RAM Role in the Actions column.
-
In the Bind/Unbind RAM Role dialog box, select the RAM role you created, then click OK.
Step 4: Add JindoFS SDK to the classpath
Add the JindoFS SDK JAR to your classpath. Use one of the following commands based on your environment:
Standard classpath:
export CLASSPATH=/xx/xx/jindofs-2.5.0-sdk.jar
Hadoop classpath:
HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/xx/xx/jindofs-2.5.0-sdk.jar
Replace /xx/xx/jindofs-2.5.0-sdk.jar with the actual path to your SDK JAR file.
Step 5: Verify password-free access
After the RAM role is attached and the classpath configured, you can access OSS using the oss:// URI scheme. The SDK fetches credentials from the instance metadata service automatically.
Shell access
Run standard HDFS commands with an OSS path:
hdfs dfs -ls/-mkdir/-put/....... oss://<ossPath>
Hadoop FileSystem API
The following example lists files in an OSS bucket by using the Hadoop FileSystem API:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import java.net.URI;
public class test {
public static void main(String[] args) throws Exception {
FileSystem fs = FileSystem.get(new URI("ossPath"), new Configuration());
RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("ossPath"), false);
while (iterator.hasNext()){
LocatedFileStatus fileStatus = iterator.next();
Path fullPath = fileStatus.getPath();
System.out.println(fullPath);
}
}
}
Replace ossPath with your actual OSS path (for example, oss://my-bucket/my-directory).