C Java Code Samples for Using the File Transfer Manager API

This appendix provides code samples for using the File Transfer Manager API. For more information, see Java API Reference for File Transfer Manager API.

Uploading a Single File

Sample Cloud account with the following details:
  • Account name: acme

    For traditional accounts, account name and identity domain name are the same.

  • REST Endpoint URL: https://acme.storage.oraclecloud.com/v1/Storage-acme
  • REST Endpoint (Permanent) URL: https://storage-7b16fede61e1417ab83eb52e06f0e365.storage.oraclecloud.com/v1/Storage-7b16fede61e1417ab83eb52e06f0e365

    Note:

    The REST Endpoint (Permanent) URL is displayed for the accounts created after November 2017.

  • For the REST Endpoint URL obtained from the REST Endpoint field:
         FileTransferAuth auth = new FileTransferAuth
               (
                "john.doe@example.com", // user name
                "password".toCharArray(), // password
                "Storage", //  service name
                "https://acme.storage.oraclecloud.com", // service URL
                "acme" // identity domain
               );
    		FileTransferManager manager = null;
    		try {
    			manager = FileTransferManager.getDefaultFileTransferManager(auth);
    			String containerName = "mycontainer";
    			String objectName = "xyz.txt";
    			File file = new File("/tmp/xyz.txt");
    			UploadConfig uploadConfig = new UploadConfig();
    			uploadConfig.setOverwrite(true);
    			uploadConfig.setStorageClass(CloudStorageClass.Standard);
    			System.out.println("Uploading file " + file.getName() + " to container " + containerName);
    			TransferResult uploadResult = manager.upload(uploadConfig, containerName, objectName, file);
    			System.out.println("Upload completed successfully.");
    			System.out.println("Upload result:" + uploadResult.toString());
    		} catch (ClientException ce) {
    			System.out.println("Upload failed. " + ce.getMessage());
    		} finally {
    			if (manager != null) {
    				manager.shutdown();
    			}
    		}
    	}
    }
  • For the Service Permanent REST Endpoint URL obtained from the REST Endpoint (Permanent) field:

    This cURL command example applies to the accounts created after November 2017.

         FileTransferAuth auth = new FileTransferAuth
                (
                  "Storage-7b16fede61e1417ab83eb52e06f0e365:john.doe@example.com", // user name
                  "password".toCharArray(), // password              
                  "https://storage-7b16fede61e1417ab83eb52e06f0e365.storage.oraclecloud.com" // service URL
                );
    		FileTransferManager manager = null;
    		try {
    			manager = FileTransferManager.getDefaultFileTransferManager(auth);
    			String containerName = "mycontainer";
    			String objectName = "xyz.txt";
    			File file = new File("/tmp/xyz.txt");
    			UploadConfig uploadConfig = new UploadConfig();
    			uploadConfig.setOverwrite(true);
    			uploadConfig.setStorageClass(CloudStorageClass.Standard);
    			System.out.println("Uploading file " + file.getName() + " to container " + containerName);
    			TransferResult uploadResult = manager.upload(uploadConfig, containerName, objectName, file);
    			System.out.println("Upload completed successfully.");
    			System.out.println("Upload result:" + uploadResult.toString());
    		} catch (ClientException ce) {
    			System.out.println("Upload failed. " + ce.getMessage());
    		} finally {
    			if (manager != null) {
    				manager.shutdown();
    			}
    		}
    
    	}
    }