Zipping Files? Oracle Database APIs Are Your Friends

We were building an ADF application hosted on JCS-SX. Among other things, the application had to create a zipped data file based on a BLOB uploaded into a DBCS table and upload the zipped file to Oracle Cloud UCM. The way we have been doing this till then was to use the ZipOutputStream Java class to create the ZIP file on the JCS-SX filesystem. We, however, ran into a problem when we had to migrate the application from JCS-SX to JCS which meant that we did not have access to the filesystem in which to create the zipped file. We could, of course, have used a separate server, SSHed into in and created the zipped file there. But that seemed too much a hassle. It is times like that that force you to reconsider your solution design and find a way out. Well, that is precisely what we did and realized much to our chagrin that we could have easily created the zipped file directly in DBCS itself using plain-old Oracle database APIs. There was no need to include the additional complexity of downloading the BLOB from the table and writing a zipped file in the filesystem.
We ended up using the APEX APIs APEX_ZIP.ADD_FILE and APEX_ZIP.FINISH to create the zipped file in DBCS itself which removed the need to interact with the filesystem. We could also have used UTL_COMPRESS.LZ_COMPRESS for the same purpose.

Moral of the story: When you happen to have a hammer, do not assume that everything can be fixed with just brute blows. Step back and think if you can do the job more easily using a screwdriver instead.

Using a Java Cloud Service (JCS) Instance as an SFTP Server

So, you happen to need an SFTP server for some quick-and-dirty work. But since you have moved your infrastructure to the cloud, you do not have anything on-premise to setup as an SFTP server. Well, there are many free as well as low-cost SFTP/SCP servers available online so you could use those.
Or, if you happen to have a Oracle Cloud Infrastructure (OCI) VM such as the one that comes with Java Cloud Service (JCS), you could set it up as an SFTP server.
Since the VM itself if a Unix-based system, you can configure it as an SFTP server like you would normally do for any *nix system. The steps for this are pretty much straightforward and are listed below:

  1. Login to the VM from the opc user.
  2. Change to the root user
    sudo su
  3. Create a new group for the SFTP user
    groupadd sftp_users
  4. Create a SFTP user and assign it to this group
    useradd -g sftp_users -d /myadmin -s /sbin/nologin myadmin
  5. Create SFTP root directory for the user
    mkdir -p /home/myadmin/upload
  6. Change ownership of the base directory to root
    chown -R root:sftp_users /home/myadmin
  7. Change ownership of the SFTP directory to the SFTP user
    chown -R myadmin:sftp_users /home/myadmin/upload
  8. Change permissions on the directories
    chmod 750 /home/myadmin
    chmod 750 /home/myadmin/upload
  9. Edit the sshd_config file. The changes you need to make here might differ slightly based on the VM you are using but the steps below should give you the general idea.
    vi /etc/ssh/sshd_config
  10. Edit the following parameter from no to yes
    PasswordAuthentication yes
  11. Edit the SFTP subsystem entry so that it is as shown below
    #Subsystem sftp /usr/libexec/openssh/sftp-server
    Subsystem sftp internal-sftp
  12. Add the user to AllowUsers
    AllowUsers opc oracle myadmin
  13. Add the following lines at the end of the file
    Match Group sftp_users
    ChrootDirectory /home/%u
    X11Forwarding no
    AllowTCPForwarding no
    ForceCommand internal-sftp
  14. Restart ssh services
    service sshd restart
  15. Add password to the SFTP user
    passwd myadmin
  16. The SFTP server is ready. You can access it using the IP address which you can see in the Cloud Console.