Esempio n. 1
0
        public static void put(String srcFileOnLocal, String destFileOnAqua)
        {
            // step 1. instantiate a client to connect to Aqua
            String        rootURL       = "http://*****:*****@172.16.20.131:8080/aqua/rest/cdmi";
            String        homeContainer = "/hongquan/test999";
            String        jsonProps     = "";
            AquaClientRef client        = new AquaClientRef(rootURL, homeContainer, jsonProps);

            try {
                // step 2. open the output file on local disk to save
                FileStream fcIn = null;
                Console.WriteLine("opening external input file: " + srcFileOnLocal);

                fcIn = new FileStream(srcFileOnLocal, FileMode.Open);

                // step 3. allocate a ByteBuffer dataBuf. be aware this buffer must be allocated via direct mode
                int    buffSize = 100 * 1024;
                byte[] dataBuf  = new byte[buffSize];

                // step 4 create the new data object on Aqua
                Console.WriteLine("creating " + destFileOnAqua + " on Aqua");

                client.nonCdmi_CreateDataObject(destFileOnAqua, "", dataBuf, 0);

                // step 5. copy the data buffer by buffer and save to the local file
                int len    = 0;
                int offset = 0;

                // the output string parameters from API nonCdmi_ReadDataObject()
                StringBuilder location    = new StringBuilder();
                StringBuilder contentType = new StringBuilder();

                // the loop
                do
                {
                    // step 5.1 clean up the data left in the dataBuf
                    Array.Clear(dataBuf, 0, dataBuf.Length);
                    // step 5.2 read the data from the local file
                    len = fcIn.Read(dataBuf, 0, dataBuf.Length);

                    // step 5.3 check the len of data read, see if we have reached EOF
                    if (len <= 0)
                    {
                        break;
                    }

                    // step 5.4 invokes nonCdmi_UpdateDataObject() to write the buffer into the data object on Aqua
                    Console.WriteLine("copying " + offset + "+" + len);
                    int ret = client.nonCdmi_UpdateDataObject(destFileOnAqua,
                                                              location, "", offset, -1, dataBuf, len, true, false);

                    // step 5.6 check the return code
                    if (ret < 200 || ret >= 300)
                    {
                        break;
                    }

                    // step 5.7 step the offset for next round
                    offset += len;
                } while (len > 0);

                // step 6. finish putting, close the local file
                Console.WriteLine("file copied, size=" + offset);
                fcIn.Close();

                // step 7. flush the data in cache onto Aqua if there are any
                client.flushDataObject(destFileOnAqua);
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message + " in the specified directory.");
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
            }
        }