in oss/lib/probe.go [1170:1334]
func (pc *ProbeCommand) probeUpload() error {
upMode := pc.pbOption.upMode
if upMode == "" {
upMode = normalMode
} else {
if upMode != normalMode && upMode != appendMode && upMode != multipartMode {
return fmt.Errorf("probeUpload errro,invalid mode flag:%s", upMode)
}
}
if pc.pbOption.bucketName == "" {
return fmt.Errorf("probeUpload error,bucketName is not exist")
}
endPoint, _ := pc.command.getEndpoint(pc.pbOption.bucketName)
if endPoint == "" {
return fmt.Errorf("probeUpload error,endpoint is not exist")
}
pSlice := strings.Split(endPoint, "//")
if len(pSlice) == 1 {
endPoint = pSlice[0]
} else {
endPoint = pSlice[1]
}
pingPath := endPoint
objectName := pc.pbOption.objectName
srcFileName, err := pc.getFileNameArg()
if err != nil {
return fmt.Errorf("probeUpload errro,getFileNameArg error:%s", err.Error())
}
var bDeleteLocalFile = false
fileSize := int64(0)
if srcFileName == "" {
// it is absolute path
currentDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("probeUpload errro,os.Getwd error:%s", err.Error())
}
uniqKey := strconv.FormatInt(time.Now().UnixNano(), 10) + "-" + randStr(10)
tempName := objectPrefex + uniqKey
srcFileName = currentDir + string(os.PathSeparator) + tempName
_, err = os.Stat(srcFileName)
if err == nil {
return fmt.Errorf("temp file exist:%s,please retry", srcFileName)
}
// prepare a local file
var textBuffer bytes.Buffer
for i := 0; i < 10240; i++ {
textBuffer.WriteString("testossprobe")
}
err = ioutil.WriteFile(srcFileName, textBuffer.Bytes(), 0644)
if err != nil {
return fmt.Errorf("prepare temp file error,%s", err.Error())
}
bDeleteLocalFile = true
fileSize = int64(textBuffer.Len())
} else {
fStat, err := os.Stat(srcFileName)
if err != nil {
return fmt.Errorf("%s not exist,stat error:%s", srcFileName, err.Error())
}
if fStat.IsDir() {
return fmt.Errorf("%s is dir,not file", srcFileName)
}
fileSize = fStat.Size()
}
var bDeleteObject = false
if objectName == "" {
uniqKey := strconv.FormatInt(time.Now().UnixNano(), 10) + "-" + randStr(10)
objectName = objectPrefex + uniqKey
bDeleteObject = true
} else {
pc.pbOption.ulObject = objectName
}
// judge object is exist or not
bucket, err := pc.command.ossBucket(pc.pbOption.bucketName)
if err != nil {
return fmt.Errorf("probeUpload ossBucket error:%s", err.Error())
}
isExist, err := bucket.IsObjectExist(objectName)
if err != nil {
return fmt.Errorf("probeUpload IsObjectExist error:%s", err.Error())
}
if isExist {
if bDeleteObject {
return fmt.Errorf("oss temp object %s exist,please try again", objectName)
} else {
bConitnue := confirm(objectName)
if !bConitnue {
return nil
}
}
}
fmt.Printf("begin parse parameters and prepare file...[√]\n")
fmt.Printf("begin network detection...")
pc.ossNetDetection(pingPath)
fmt.Printf("\rbegin network detection...[√]\n")
fmt.Printf("begin upload file(%s)...", upMode)
// begin upload
startT := time.Now()
if upMode == appendMode {
err = pc.probeUploadFileAppend(srcFileName, objectName)
} else if upMode == multipartMode {
err = pc.probeUploadFileMultiPart(srcFileName, objectName)
} else {
err = pc.probeUploadFileNormal(srcFileName, objectName)
}
endT := time.Now()
var logBuff bytes.Buffer
if err == nil {
fmt.Printf("\rbegin upload file(%s)...[√]\n", upMode)
logBuff.WriteString("\n************************* upload result *************************\n")
logBuff.WriteString("upload file:success\n")
logBuff.WriteString(fmt.Sprintf("upload file size:%d(byte)\n", fileSize))
logBuff.WriteString(fmt.Sprintf("upload time consuming:%d(ms)\n", endT.UnixNano()/1000/1000-startT.UnixNano()/1000/1000))
logBuff.WriteString("(only the time consumed by probe command)\n\n")
if pc.pbOption.ulObject != "" {
logBuff.WriteString(fmt.Sprintf("upload object is %s\n", pc.pbOption.ulObject))
}
} else {
fmt.Printf("\rbegin upload file(%s)...[x]\n\n", upMode)
logBuff.WriteString("\n************************* upload result *************************\n")
logBuff.WriteString("upload file:failure\n")
logBuff.WriteString("\n************************* error message *************************\n")
logBuff.WriteString(fmt.Sprintf("%s\n", err.Error()))
}
fmt.Printf("%s", logBuff.String())
pc.pbOption.logFile.WriteString(logBuff.String())
fmt.Printf("\n************************* report log info*************************\n")
fmt.Printf("report log file:%s\n\n", pc.pbOption.logName)
// delete oss temp object
if bDeleteObject {
pc.deleteObject(objectName)
}
// delete local file
if bDeleteLocalFile {
os.Remove(srcFileName)
}
return err
}