Exemple #1
0
func UploadImage(fileURL, fileName, containerName string) bool {
	cDallas := swift.Connection{
		UserName: "",
		ApiKey:   "",
		AuthUrl:  "https://auth.api.rackspacecloud.com/v1.0",
		Region:   "DFW",
	}
	if err := cDallas.Authenticate(); err != nil {
		log.Println("Couldn't authenticate with rackspace Dallas")
		return false
	}
	var rspaceHeader swift.Headers
	fDallas, err := cDallas.ObjectCreate(containerName, fileName, false, "", "image/jpg", rspaceHeader)
	if err != nil {
		log.Println("Failed to create rackspace object ", err)
		return false
	}
	defer fDallas.Close()
	d, err := ioutil.ReadFile(fileURL)
	if err != nil {
		log.Println("Failed to read source file ", err)
		return false
	}
	log.Println("uploading path:", fileURL)
	if _, err := fDallas.Write(d); err != nil {
		log.Println("Failed to write file to rackspace Dallas server", err)
		return false
	}
	return true
}
func UploadFile(container string, prefix string, path string, c swift.Connection) {
	dat, err := ioutil.ReadFile(path)
	if err != nil {
		println(err.Error())
	} else {
		name := filepath.Base(path)
		if len(prefix) > 0 {
			name = prefix + "-" + name
		}
		ext := filepath.Ext(path)
		hasher := md5.New()
		hasher.Write(dat)
		md5hash := hex.EncodeToString(hasher.Sum(nil))

		fmt.Printf("Uploading %s to container %s\n", name, container)
		file, err := c.ObjectCreate(container, name, false, md5hash, ext, nil)
		if err != nil {
			println(err.Error())
		} else {
			file.Write(dat)
		}
		file.Close()
	}
}
Exemple #3
0
func worker(c swift.Connection, parent string, level int) string {
	dirs, err := ioutil.ReadDir(parent)

	if nil != err {
		fmt.Println(err.Error())
	}

	var logstr string
	for _, f0 := range dirs {
		var curdir string
		if prefixlen == len(parent) {
			curdir = f0.Name()
		} else {
			curdir = parent[prefixlen:len(parent)]
			curdir = strings.TrimLeft(curdir, "/")
		}

		//		fmt.Println(parent, level, curdir)

		switch {

		case f0.IsDir():
			if 0 == level {
				logstr = fmt.Sprint(strings.Repeat("\t", level)+"Found Container Creating...", curdir)
				fmt.Println(logstr)
				if err = c.ContainerCreate(curdir, headers); err != nil {
					panicString := fmt.Sprint("PutContainer Error:", err)
					panic(panicString)
				}
			}
			worker(c, parent+"/"+f0.Name(), level+1)
		case !f0.IsDir():
			if strings.HasPrefix(f0.Name(), ".") {
				logstr = fmt.Sprint("Found Hidden Object. Ommited ", f0.Name(), " to ", curdir)
				fmt.Println(logstr)
				continue
			}

			contents, _err := ioutil.ReadFile(parent + "/" + f0.Name())
			if nil != _err {
				panicString := fmt.Sprint("PutObject Error:", _err)
				panic(panicString)
			}

			h := sha1.New()
			h.Write(contents)
			obj_sha1hash := hex.EncodeToString(h.Sum(nil))
			logstr = fmt.Sprint(strings.Repeat("\t", level)+"Found Object. Uploading ", f0.Name(), " [ ", obj_sha1hash, " ] to ", curdir)
			fmt.Println(logstr)

			fstream, err := c.ObjectCreate(curdir, f0.Name(), false, "", "application/zip", headers)
			if nil != err {
				panicString := fmt.Sprint("PutObject Error:", err)
				panic(panicString)
			}

			if counts, err := fstream.Write(contents); nil != err {
				panicString := fmt.Sprint("PutObject Error:", err)
				panic(panicString)
			} else {
				fmt.Printf("%vBytes Total: %v, Written: %v\n", strings.Repeat("\t", level), len(contents), counts)
			}

			fstream.Close()

			return ""
		}
	}
	return ""
}