Upload csv file at Google Cloud Storage using java

First do all required pre requiste as per google doc
https://cloud.google.com/appengine/docs/java/googlestorage/

Now see the sample code for a csv file uploader using latest api

public String uploadFileUsingGCSClient(String csvDate,
String fileName, String dirName,String bucketName) throws IOException {

 // init the bucket access
    GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
    GcsFilename filename = new GcsFilename(bucketName+"/"+dirName, fileName);
    GcsFileOptions fileOptions = new GcsFileOptions.Builder()
   .mimeType("application/CSV")   
   .acl("public-read")
   //.addUserMetadata("myfield1", "my field value")
   .build();
    GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, fileOptions);
    
    // write file using this stream
    BufferedOutputStream outStream = new BufferedOutputStream(Channels.newOutputStream(outputChannel));
    
    // read the input stream
    byte[] buffer = new byte[1024];
    //List<byte[]> allBytes = new LinkedList<byte[]>();
    InputStream reader = new ByteArrayInputStream(csvDate.getBytes());
    while(true) {
        int bytesRead = reader.read(buffer);
        //log.info("bytesRead:"+bytesRead);
        if (bytesRead == -1) {
            break; // have a break up with the loop.
        } else if (bytesRead < 1024) {
            byte[] temp = Arrays.copyOf(buffer, bytesRead);
            outStream.write(temp);
           
        } else {
        outStream.write(buffer);
        }
    }

    outStream.close();
    outputChannel.close();
    String uploadedFileURL="gs://"+bucketName+"/"+dirName+"/"+fileName;
    log.info("uploadedFileURL : "+uploadedFileURL);
    return uploadedFileURL;
  }

Popular posts from this blog

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Upload file on Google cloud storage using Java servlet on google app engine

Android: where to run keytool command in android