Call getFile()
method to access the content of a file. The call returns an object that contains an InputStream for accessing the file’s content and a method for accessing the file’s Content-Type. It is the caller’s responsibility to close the InputStream when finished."
Code Sample
public InputStream fileDownload(OAuthToken token, String path)
{
CloudApp app = new CloudApp("appname", "key", "secret", "redirectURI");
CloudClientContext context = new CloudClientContext(app, token);
CloudClient client = new CloudClient(context);
try
{
ContentTypedStream item = client.getFile(path);
System.out.println("mime-type: " + item.getContentType());
return item.getContent();
}
catch (CloudHttpException e) {
System.out.println("bad HTTP response");
}
catch (IOException e) {
System.out.println("network error, or failure to interpret json-encoded response");
}
return null;
}
While uploading files which are only a few hundred of kilobytes in size on a standard connection typically does not pose any difficulties, this often might not be the case for your users. Your app should be able to upload large files, such as videos efficiently. It should also be able to recover from interrupted uploads that might be caused by mobile connectivity issues or power outages, without having to restart the upload process.
Chunked file uploads allow you to break a large file into small chunks, and then send these pieces to the upload server one-by-one. If an upload were to fail, your app would not have to resend the whole file. It can resume the file upload from the last successfully sent chunk.
NOTE:
You cannot perform the file upload operation on a virutal folder.
Checksum
The file upload methods require a checksum to ensure that the binary content is transferred correctly. Java SDK contains Checksum
utility to calculate SHA-256 a hash for checksum.
Refer to Calculating Checksum document for the detailed information on calculating checksum.
Use this approach only for very small files.
To upload a file:
getFileUploadIntent
to indicate your intent to upload a file. The response from this method includes a URL to be used for the upload.postFileUpload
to upload the actual file content.Code Sample
public void upload(OAuthToken token, String path, String name, int size, File file) throws IOException
{
CloudApp app = new CloudApp("appname", "key", "secret", "redirectURI");
CloudClientContext context = new CloudClientContext(app, token);
CloudClient client = new CloudClient(context);
// Calculate checksum
String checksum = Checksum.SHA256(file);
try (InputStream is = new FileInputStream(file))
{
// obtain an upload URL
FileUploadIntent fileuploadIntent = client.getFileUploadIntent(path, name, size, checksum, false);
String uploadURL = fileuploadIntent.getUploadURL();
// upload file content
client.postFileUpload(uploadURL, is, size, checksum);
}
catch (CloudHttpException e)
{
System.out.println("bad HTTP response");
}
catch (IOException e)
{
System.out.println("network error, or failure to interpret json-encoded response");
}
}
Use this approach for most file uploads.
To upload a file:
getFileUploadIntent
to indicate your intent to upload a file. The response from this method includes a URL to be used for the upload, and a URL to be used to commit the file once all data has been uploaded.postFileChunk
for each chunk of data to be uploaded. In the sample code below, the utility class PartialInputStream
is used to help divide the file into chunks.postCommit
to indicate that the whole file has been uploaded.Code Sample
public void uploadChunked(OAuthToken token, String path, String name, int fileSize, long chunkSize, File file) throws IOException
{
CloudApp app = new CloudApp("appname", "key", "secret", "redirectURI");
CloudClientContext context = new CloudClientContext(app, token);
CloudClient client = new CloudClient(context);
// Calculate checksum
String checksum = Checksum.SHA256(file);
try (InputStream is = new FileInputStream(file))
{
// obtain upload URL & commit URL
FileUploadIntent fileuploadIntent = client.getFileUploadIntent(path, name, fileSize, checksum, true);
String uploadURL = fileuploadIntent.getUploadURL();
String commitURL = fileuploadIntent.getCommitURL();
// Upload file content
int chunk = 0;
for (long offset = 0L; offset < fileSize; offset += chunkSize)
{
long uploadSize = Math.min(fileSize - offset, chunkSize);
PartialInputStream partStream = new PartialInputStream(is, uploadSize);
client.postFileChunk(uploadURL, partStream, ++chunk, uploadSize);
}
// commit the upload
client.postCommit(commitURL);
}
catch (CloudHttpException e)
{
System.out.println("bad HTTP response");
}
catch (IOException e)
{
System.out.println("network error, or failure to interpret json-encoded response");
}
}
The getFullview
method returns the metadata for all files and folders in the end user's Verizon Personal Cloud Storage storage account. This is useful for applications that have a need to persist end-user’s file data for future use.
Responses from calls to getFullview
are unbounded and can be quite large. In production applications, it is more practical to use getFullviewStreaming
, which returns a StreamingFullview object. StreamingFullview
class allows you to iterate through an end-user’s file and folder content while minimizing memory usage as shown in the following example:
Code Sample
public String fullviewStreaming(OAuthToken token, String etag)
{
CloudApp app = new CloudApp("appname", "key", "secret", "redirectURI");
CloudClientContext context = new CloudClientContext(app, token);
CloudClient client = new CloudClient(context);
try
{
// pass in the etag value from a previous call to fullview() to receive a list of deltas.
// note: if etag is null, all files and folder will be included in the response.
FullviewStreaming fullview = client.getFullviewStreaming(etag);
// print out information about adds and deletes.
for (FullviewCmd cmd : fullview.commands())
{
switch (cmd.cmdType())
{
case files:
FullviewFileCmd fileCmd = (FullviewFileCmd) cmd;
System.out.println("The file " + fileCmd.getParentPath() + "/" + fileCmd.getName() + " was added.");
break;
case folders:
FullviewFolderCmd folderCmd = (FullviewFolderCmd) cmd;
System.out.println("The folder " + folderCmd.getParentPath() + "/" + folderCmd.getName() + " was added.");
break;
case deleted:
FullviewDeletedCmd delCmd = (FullviewDeletedCmd) cmd;
System.out.println(delCmd.getPath() + " was deleted.");
break;
}
}
// save etag for later use.
return fullview.getEtag();
}
catch (CloudHttpException e)
{
System.out.println("bad HTTP response");
}
catch (IOException e)
{
System.out.println("network error, or failure to interpret json-encoded response");
}
return null;
}
Obtains metadata for files and folders in a user's folder. Response class for the metadata API. Contains a single item's metadata, either a file or a folder (including the folder's contents).
The following example illustrates a call to getMetadata
endpoint.
Code Sample
public Metadata getMetadata(HttpSession session)
{
CloudClient client = getClient(session);
Metadata metadata = client.getMetadata(path, includeDeleted, sort, start, count, filter);
return metadata;
}
Use getThumbnail
method to retrieve thumbnails for an image file.
Code Sample
public InputStream thumbnails(OAuthToken token, String path) throws IOException
{
CloudApp app = new CloudApp("appname", "key", "secret", "redirectURI");
CloudClientContext context = new CloudClientContext(app, token);
CloudClient client = new CloudClient(context);
// obtain a content token for the file
String contentToken;
{
Metadata metadata;
try
{
metadata = client.getMetadata(path, false, (String) null, null, null, null);
}
catch (CloudHttpException e)
{
if (e.getStatusCode() == 404)
System.out.println("File doesn't exist");
else
System.out.println("bad HTTP response");
return null;
}
assert metadata != null;
if (metadata.getFile() == null)
{
System.out.println(path + " is not a file");
return null;
}
contentToken = metadata.getFile().getContentToken();
}
// Return an InputStream containing a jpeg-encoded thumbnail image, using the file's content token.
// if the file isn't an image, CloudHttpException will be thrown.
try
{
// ThumbnailSize.m returns a 128x128 pixel image. You can also specify a custom image size, eg:
// client.getThumbnail(contentToken, null, 640, 640);
// However performance will be impacted.
return client.getThumbnail(contentToken, ThumbnailSize.m, 0, 0);
}
catch (CloudHttpException e)
{
System.out.println("bad HTTP response");
return null;
}
}
Permalink
sfre0003 Scott Frenkiel added a comment - 26/Jul/16 5:44 PM - Restricted to jira-users
Copyright © 2015-2017, Verizon and/or its Licensors. All rights reserved.