package com.xiaomi.infra.galaxy.fds.android.example; import java.io.ByteArrayInputStream; import java.io.InputStream; import com.xiaomi.infra.galaxy.fds.android.FDSClientConfiguration; import com.xiaomi.infra.galaxy.fds.android.GalaxyFDSClient; import com.xiaomi.infra.galaxy.fds.android.GalaxyFDSClientImpl; import com.xiaomi.infra.galaxy.fds.android.auth.GalaxyFDSCredential; import com.xiaomi.infra.galaxy.fds.android.auth.OAuthCredential; import com.xiaomi.infra.galaxy.fds.android.model.FDSObject; import com.xiaomi.infra.galaxy.fds.android.model.ObjectMetadata; import com.xiaomi.infra.galaxy.fds.android.model.PutObjectResult; public class FDSOAuthClient { // This is a simple demo for FDS android SDK, note that before use android // sdk, you should do the following preparation: // 1. Create the bucket which you want to upload your object // 2. Grant proper bucket permission for your 'app' // Configure your oauth related parameters here private static final String FDS_BASE_URI = "https://files.fds.api.xiaomi.com"; private static final String APP_ID = "your_app_id"; private static final String OAUTH_APP_ID = "your_oauth_app_id"; private static final String OAUTH_PROVIDER = "XiaoMi"; private static final String OAUTH_MAC_ALGORITHM = "HmacSHA1"; private static final String OAUTH_MAC_KEY = "your_oauth_mac_key"; private static final String OAUTH_ACCESS_TOKEN = "your_oauth_access_token"; private static final String BUCKET_NAME = "your_bucket"; public static void main(String[] args) throws Exception { GalaxyFDSCredential credential = new OAuthCredential(FDS_BASE_URI, APP_ID, OAUTH_APP_ID, OAUTH_ACCESS_TOKEN, OAUTH_PROVIDER, OAUTH_MAC_KEY, OAUTH_MAC_ALGORITHM); FDSClientConfiguration config = new FDSClientConfiguration() .withCredential(credential); GalaxyFDSClient client = new GalaxyFDSClientImpl(config); // Put an object to a specified bucket with a given name, if the object // already exists, it will be overrided. String objectContent = "This is a simple test object"; InputStream inputStream = new ByteArrayInputStream( objectContent.getBytes()); String objectName = "text-object" + System.currentTimeMillis(); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType("plain/text"); metadata.setContentLength(objectContent.length()); client.putObject(BUCKET_NAME, objectName, inputStream, metadata); // Download the object with the bucket name and object name specified FDSObject object = client.getObject(BUCKET_NAME, objectName); byte[] buffer = new byte[objectContent.length()]; object.getObjectContent().read(buffer); System.out.println(new String(buffer)); // Put an object to a specified bucket, the server will generate an unique // object name for the object. inputStream = new ByteArrayInputStream(objectContent.getBytes()); PutObjectResult result = client.putObject(BUCKET_NAME, inputStream, metadata); System.out.println(result.getObjectName()); // Download the object with the bucket name and object name specified object = client.getObject(BUCKET_NAME, result.getObjectName()); buffer = new byte[objectContent.length()]; object.getObjectContent().read(buffer); System.out.println(new String(buffer)); } }